r/PHPhelp • u/NoTomorrow7454 • Dec 03 '22
Is a framework necessary to create a PHP project?
I understand that a framework can help me structure my project and give it security. I also understand that if you don't know how to use the framework well, it can take you more time to make a project than to do it manually, but leaving that aside and assuming that we know how to use the framework (example: symfony, laravel...), professionally for your own personal project, is it better to do it from scratch, or using what was mentioned above?
Really the intrigue that I have is if using the framework is equal to more security, or is equal to speeding up the process of creating the project.
So I ask these questions:
Using a framework equals more security? Can we have the same security doing it from 0? Is it worth using a framework even knowing that we are not the owner of all the code?
2
u/future-tech1 Dec 04 '22
Back in the old days, PHP had no frameworks. The code was clunky and had little to no structure. If search the web for the leaked source code of the first version of Facebook and take a look, you'll see what I mean.
Then along came frameworks like Zend and Symfony. They provided structure. This was before composer and frameworks tended to be quite bloated, offering everything they think you might need. You probably won't end up using 90% of it.
composer can now provide alot of the types of dependencies frameworks do, so its possible to build a lightweight application just with composer dependencies and no framework.
But rather than have no framework, you can take the middle ground like I did and built your own lightweight framework. I wrote a guide here on doing this: https://softwareengineeringstandard.com/2022/11/15/build-your-own-php-framework-step-by-step-part-1/
Here is the finished code: https://github.com/cipher-code/phpframework
2
12
Dec 03 '22 edited Dec 03 '22
professionally for your own personal project, is it better to do it from scratch, or using what was mentioned above?
I have a somewhat unpopular opinion on this topic for this venue, but you asked, so I am going to provide it. Sure, you can do it without a framework and everything will be just fine, however, at some point if your personal project is successful and you put a lot of time and effort to get it there without a framework, it is going to be extremely difficult to get the support of a framework into your project after the fact. So much of what I and many others have come to hate about PHP is that a lot of projects start out as something small where the benefit of a framework is negligible, but then they grow into money making enterprise projects that have no discernible architecture, no separation of concerns, no testing, no auto-loading and best of all, a lot of them are stuck on PHP 5.6 because everyone's too scared to try and update it. Of all the places I've worked at over the past 20+ years, more than 50% fall into this category. For that reason, I always advocate using some type of framework regardless of how small or insignificant a project may seem at it's inception.
Is it worth using a framework even knowing that we are not the owner of all the code?
This is kind of a bullshit argument. You are not the Owner of PHP, or it's extensions, or their dependent libraries like OpenSSL, or Nginx, or Apache, or the Linux Kernel, but you don't see people trying to write their own everything else. Most frameworks and libraries have permissive licenses like MIT / BSD meaning you don't owe the owner of that code anything for your derived work except a mention. You do not need to disclose your code and they do not own anything you derive.
4
u/dabenu Dec 03 '22
I fully agree. Having whatever framework as a basis makes future maintenance so much easier, since you can just look up the old documentation instead of having to guess whatever the idiot who built it was thinking (yes, 6 year into the future you probably consider current you an idiot. It's a good thing though, it means you learned something).
Also even GPL is very permissive and usable for proprietary projects, as long as you don't distribute them. Which in case of PHP, you probably don't do anyway.
2
u/jmp_ones Dec 04 '22 edited Dec 04 '22
Having whatever framework as a basis makes future maintenance so much easier
I'm afraid I must disagree here. I mean, you'd think this would be true, but I can tell you from painful experience it is not (or at least, it is vanishingly rare that it is true).
Quoting from my book on Modernizing Legacy Applications in PHP:
Each different public framework in PHP land is its own unique hell. Applications written in [... one framework ...] suffer from different legacy issues than those written in [ ... list of other frameworks ... ], and so on. Each of these different frameworks, and their varying work-alikes, encourage different kinds of tight-coupling in applications. Thus, the specific steps needed to refactor applications built using one of these frameworks are very different from the steps needed for a different framework.
Overall, I would much rather refactor a "typical" include-oriented page-based PHP application than one written in just about any framework. (Micro-frameworks like Slim tend to be another thing entirely.)
3
u/equilni Dec 04 '22 edited Dec 04 '22
Agreed. The more tightly coupled application to a framework is, it may be more of a hindrance than a benefit later on. It may be hard and time consuming in practice (depending on the size of the application and the framework used), but if one develops the business logic independent of frameworks - as much as possible, the better off future maintenance will be.
https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
Case in point - CI3 to CI4
CodeIgniter 4 is a rewrite of the framework and is not backwards compatible. It is more appropriate to think of converting your app, rather than upgrading it.
1
u/abrandis Dec 04 '22
I agree to a point, first off any project that because of it's success is a runaway hit , is usually clean sheet re-writen to better scale and accommodate a more complex infrastructure.. I mean the Facebook and. WordPress in use today have nothing in common with the original versions
3
u/steven447 Dec 03 '22
Using a framework equals more security? Can we have the same security doing it from 0?
Never design your own security solutions!
Popular security components have been reviewed and improved by thousands of people. Starting from scratch is the worst thing you can do.
3
u/equilni Dec 04 '22
Since there are many good answers, I will just chime in with this article: https://matthiasnoback.nl/2021/02/should-we-use-a-framework/
2
u/HolyGonzo Dec 03 '22
What Allen said, but I would say that frameworks DO provide more security -IF- you are not familiar with how to manually write secure code.
Beginners are often just looking to make their app work, and don't yet have the experience to know how to make it work securely. It's like someone who has just learned that the gas pedal makes the car go forward - they aren't even thinking about the brake pedal because that does the opposite of what they're using to do.
As long as you learn how to use the frameworks correctly, they will usually provide a lot of inherent safety in their methods, and are -usually- more secure than beginner code.
2
u/adastrasemper Dec 03 '22
It also depends on what kind of project you have in mind. Your site is as secure as your server which is provided by the hosting company. You take care of login security, have SSL etc. And without the framework it will be much much faster. Or you can look into minimalist frameworks.
2
u/liquid_at Dec 03 '22
you can do it and you can do it just as secure as with a framework. Frameworks just take a whole lot of work off your hands by providing you with industry-standard solutions that you can use.
Generally speaking, 90% of most projects is the same basic stuff like user-authentication, content-management, etc. If you can focus on the 10% that make your project special and ignore the 90% that you need every time, you can improve your output by 10x.
But if you want it fast, secure and cheap... better pick a framework and don't do it yourself.
If you just want to learn how to do stuff, doing it by hand also works, but it's not fast or reliable. You'll spend more time working around your own mistakes than to actually write new code.
1
u/am0x Dec 04 '22
No but depending on requirements may likely make it way easier.
But that’s true for all projects. Yea, everyone bitches about Wordpress but in reality it might be the best solution for a client.
1
u/g105b Dec 04 '22
I believe you should start with no framework, so you can appreciate what a framework helps (or hinders), and so you get the opportunity to evaluate the tool choice for each project.
1
u/Audience-Electrical Dec 03 '22
Nah.
I see you got some long informative answers so here's one that isn't.
1
u/illathon Dec 04 '22
Nah, PHP is pretty simple to use. Probably want a few nice libraries though. Eloquent for an ORM or some other ORM is nice.
-1
25
u/allen_jb Dec 03 '22
No, you don't need to use a framework to create a PHP project. Some of my smallest scripts have no dependencies at all outside of extensions bundled with PHP.
You also don't need to go to that extreme either tho. There are a number of "microframeworks" that contain a small amount of functionality (typically request handling, routing and some error handling, possibly with some other functionality) which can give you a good "launch pad", then you choose what libraries / components you want to use for everything else.
Composer / Packagist have make this style of development almost as easy as using a full-fat framework (easier in some ways, I would argue, especially when it comes to major version upgrades)
See also https://github.com/PatrickLouys/no-framework-tutorial
I don't think that using a framework necessarily provides more security than any of these approaches. You could argue in many cases that the extra code many frameworks come with to provide functionality that 80-90% of users will never need is in fact an unnecessary security risk.
Attempting to cater for too many cases can leave frameworks / libraries harder to use (securely / correctly). I think it's often better to have a wide range of libraries that cater to different use cases instead (and don't have to try to squash everything into a common api to do it).
Using frameworks and libraries written by others is still worth it tho - it's code you don't have to maintain yourself, and can, certainly in the cases of more popular frameworks / libraries, rely on to be relatively free of bugs and security issues.
However this can come with a cost when you need to do major version upgrades, particularly of larger frameworks like Laravel that may require many changes to many different parts of your code for the same upgrade and may have decided to completely rework parts of the framework between major versions, altho tools such as Rector can help with this.