PHP has garnered a bad reputation in the software market. Many developers hold strong opinions against the language and to some extent, that's correct. Over the last few years, although PHP has gone through an evolution and is it the same "Fractal of Bad Design"?
This post was originally made to my blog: Twodee's Kitchen. I would love for you to visit that and show some love.
Yes, I admit it. PHP sucks.
And I shamelessly write PHP code, so I must suck too, right?
As popularly stated in the famous article PHP: a fractal of bad design :
PHP is an embarrassment, a blight upon my craft. Itβs so broken, but so lauded by every empowered amateur whoβs yet to learn anything else, as to be maddening. It has paltry few redeeming qualities and I would prefer to forget it exists at all.
The article has only gotten popular over the years, and it's still being circulated over Quora (One of them being the founder himself, Adam D'Angelo).
And while the article came out, yes, it was right. It was badly designed and badly implemented. The users made it worse. While PHP came along, it didn't originally plan to be as massively used as it is today. Since it was so easy to adapt to, people started using it everywhere. It worked to some extent.
As soon as the web got more popular, we changed, our needs changed. PHP didn't. It was still stuck behind, probably due to the community? It was inconsistent with its naming, it had the insecure mysql_*
functions built into its design. There were a lot of gotchas in the language, and having used PHP for a long time, I know that it is a pain.
But this is 2019. The article was written in 2012. I am surprised people still keep quoting that article everywhere!
C'mon people, PHP has changed (evolved) a lot, don't tell me the public eye is too blind to see it.
PHP has had some major pushes like the HipHop to HHVM movement from Facebook and PHP 7. Developers have recognized the issues that came along with it, and they have been addressing it so far. If you are living in 2019 and still writing mysql_*
functions (or blaming PHP for having that), you seriously need to learn to RTFM.
So why this hatred still?
Well, as long as something's popular - people will hate it. People hate Java, people hate C++. When millions use your product, you cannot expect everyone to be happy customers with every design decisions that you make. There will be people who don't like your approach and that's true for any remotely popular language.
People often compare Python to PHP in the web context. I have nothing against Python, I think it's a great language that fits the purposes it was intended for (scripting?) and purposes it was adapted popularly for (AI/ML/Data Science?).
But here's where Python fails to impress me against PHP:
It's slow. Not a deal-breaker (especially on the web), but I am making arguments for the sake of making arguments. If you still complain about PHP being ugly, I can complain it about being slow.
It needs a framework for anything web. Initially, when I just wanted to get a Python application up and running for the web, I had the community continuously push me over to use Django or Flask. I hate being coupled to a framework, as many others would (and should) be too.
Whitespacing? Not a fan. Again, it's not a deal-breaker, but having whitespace mean something doesn't make sense to me. I understand that it was a design decision to keep the lines cleaner, but when things break because I missed an invisible whitespace - it hurts my feelings.
Its Object-oriented model is alien to me. Access specifiers are done by enforcing conventions with underscores? Okay. No. Maybe it works for some folks, but I like things being implied explicitly rather than implicitly.
But okay, Python is a great language. It works great, it's got a great community and yet - there are people who hate it. And that's okay if you are remotely popular anywhere - you will have people not liking you.
Javascript on the other hand - It's something I really don't like. It's a matter of personal opinion. Especially after the fact that some Javascript dudes who are a few years older than me were trying to shove Node.js down my throat and bashing PHP for the time I was in front of them. Asserting that Node is far superior, safe and faster than PHP (and anything else for the web) and I should learn Node right away.
They went as far as saying that PHP invented SQL injection. I stopped trying to speak at that point.
Coming back to PHP:
Do you still have/write legacy PHP code that follows the arcane PHP 5 approach?
I have a list for you:
Start writing OOP
While you can still write procedural PHP, the community has moved towards an object-oriented approach. It simply fits the new model and works great to have you structure the code well. With object-oriented, several clean coding practices like SOLID and DRY are automatically implied.
OOP silently enforces clean structuring of your codebase and keep things separated better. Of course, it's optional, if you like writing spaghetti code, no one's stopping you. You can make the worst out of PHP and give yourself a bad name. But that's entirely up to you, any language will allow you to do that. Not just PHP.
Strict type as much as you can
While we are at the subject, also use Strict Types. It's as simple as: <?php declare(strict_types=1)
at the beginning. Static typing surely helps you keep things consistent and safe. Yes, PHP is a dynamically typed language, and we need to squeeze that feature out of PHP sometimes. But in most cases, going by the safer path with strict types can save you from a lot of weirdness and unpredictability at runtime.
Namespaces, please
The include statements on top of the page are no more common and the community recommends that you use namespaces to "import" modules you need to "use". Its a means of abstraction over your raw PHP files that allows you to encapsulate the include logic.
Yes, it could be weird to use \
as the namespace separator at first, but you'll get used to it. Get rid of those includes and start using namespaces to put things into their correct places. Which brings me to my next point:
Composer
If you are planning to start a PHP project, get composer immediately. It's a dependency management tool which allows you to define your dependencies, your application and test entry points and load dependencies from the central Packagist repository as you need them. It generates an autoloader for you, and that's the only thing you should include
in your entire project.
Throw away mysql_*
All the mysql_*
functions have been deprecated for a long, long time and it has been removed in PHP 7 for good. So if you are still complaining about mysql_*
functions being bad, please upgrade your PHP version. The best way to deal with a database as of now is to use PDO
with prepared statements. It's a generic API that works quite well with a vast array of databases.
The things that I like about PDO are: it's clean, relatively modern design, object-oriented and consistent. You will move to exclusively use PDO in no time once you start a project with PDO.
Again, don't create DB wrappers like DBConnection extends PDO
. Just don't. If you need some sort of abstraction over PDO, check out the Data Mapper pattern and some ORM like Doctrine
(and not some evil Active Record variant).
Separate your concerns
Most of the bashing PHP gets today is because new developers mess it up so bad, that it smells worse than Javascript (I am opinionated, sorry). Because its easy to learn and get started with, newbies just can't resist themselves from writing hacky code and deploying it to production.
Other languages don't get this because
a) they have a steeper learning curve
b) they give out a strict design strategy beforehand.
c) It takes time to get them up and running.
Fix this by separating your concerns. I remember once I used to copy-paste portions of long functions to other files to do the same thing, but slightly differently. I understand why beginners do that.
Start off by making your functions smaller.
Break your codebase down to small pieces acting independently doing exactly one thing. Read more on SOLID and DRY principles.
If you have a User
class, don't allow it to be able to create a message, encrypt the message and also send the message to another user.
Incorporate libraries from the internet, people solved your problems with better testing well before you did. And they did it better. Whilst we may be tempted to have everything custom made, we tend to deviate from the actual business needs. Focus on your business logic, use what's already available.
But no tight coupling.
PSRs to the rescue
And finally, follow coding conventions and read on PHP-FIG. The PHP Standards Recommendations (PSRs) will allow you to have a consistent codebase that others can easily understand and extend. The standards will help you to write code that can be compatible with other code written by others, and that will save you from cursing yourself 3 months after you write some bad PHP code.
This recommendation applies to every language in general. Follow coding conventions and strive to write better code. Of course, no one can stop you from being a "code-rebel". We don't have the technology to stop you from that yet.
(Yes we do, coding standards checks during integrations can keep idiots at bay)
A few concluding words
Yes, PHP sucks. So does every other language. You just gotta deal with it.
It's up to you and your team to write code that looks like poetry rather than an ugly war cry. You can write severely bad Java code even with its verbosity and static typing.
You were scared about starting your next project with PHP because your coworkers will judge? Go right ahead and do that. It's your job and it's your tool.
Your coworkers are probably too free, maybe their code is still compiling.
Top comments (41)
Great summary. I still use PHP. In fact, our API is running on PHP Slim 3 framework. PSR, Autoloading, Composer - all great improvements to the PHP community. My biggest complaint historically has been inconsistent argument order across similar methods, Strings in particular.
I also love that PHP is open-ended. There's more than one way to skin a cat.
I like that I can run PHP on any web server of my choosing - let's say Apache running on Event MPM, or nginx.
Right, inconsistency has been a major issue with PHP and it needs to be addressed. It sometimes gets annoying.
But again, remembering every argument order would be hard in any language. The manual or a nice IDE/Text editor should make up for it. PHPStorm does a really good job.
The oft-cited issue of $needle & $haystack order is moot. It's one way for array functions & one way for string functions. != inconsistent.
need and haystack are used for search functions. The very thinking in 'array' or 'string' functions as PHP has it is just proof of the madness baked in the language. We are talking search functions that happen to apply to arrays or strings. If there's a good reason for them to be that way, I'd love to hear it.
So you're saying the functions should overload their purpose & accept both string or array haystacks?
PHP is the best language out there. Whoever says otherwise is just crazy or just doesn't have enough imagination. You can literally do anything you can think of with it.
What the? I'm curious, what make you feel so strongly? There are a ton of fantastic lower and higher level languages to try, does it interest you to try something else. You might find the experience helps your writing better code in general.
But of course i'm going to use other languages as well. And it does help in writing better code knowing other languages but I've used many languages both in web and game development (C++, obj C, java) and even various js frameworks like vue, nodejs etc but for some reason i always end up using php either as a backend or for logic and data manipulation and db interaction. Why? Because it is way faster, cleaner and easier to do all that compared to the rest. Try handling db queries with Vue, java or Obj C...and i am not talking about simple Selects here, but creating db queries based on user entries where you have tons of if/else/cases before deciding the ending query. I was working on a big project with a search form that had 50+ fields, combining 20+ tables, and 2 databases to bring back results. How are you going to handle all those cases if not with PHP? How many days would it take you to write that code using any other language?
Let me give you another example. My phone company gives me about 300min of mobile calls free of charge per month. It has no way of notifying me when i go over the limit (and why would they), so i had to think of an easy way of doing that myself. I wrote a parser that logs in my web account, checks my balance, uploads the stats in a db for a graph i made, then decides whether to notify me if i am approaching my limit etc via sms, email or push notification depending if my phone is under the same network or not. This thing uses crons, bash scripts, mysql, and also provides a GUI for various tweaks like check intervals the graph etc, all controlled via php. How long is the code? about 100 lines of code, done in a few hours. How would do all the above if not with PHP? How long would it take you, and how many different languages would you use?
You want another example ? There was a project that had a huge db of addresses that were stored in textareas (entries from an old system) and needed to have them separately each part in a different column, like zip code, address, area, country etc. There are some libraries out there that can do that, but you'd have to load an .so or .dll on the server first and that was not doable at the time. I wrote a parser that would break down a db entry into parts then try to figure out which is which using A.I algorithms to predict what is a street address, what is a zip code etc, and if it couldnt find or it wasnt sure about something, it would google it and then from the page results it will figure out if it's a city, a village, a country, a zip code, a phone number....whatever...and then act accordingly....yes..using PHP and it had 100% success at separating any entry you threw at it. I am sorry but how would you solve this problem using any other language?
I am not a PHP hater, I am not a hater period. I don't know if I agree, because subjectivity, I feel that Rust is the best language out there... For specific reasons. PHP is probably the best server language for PHP based servers. As for your numerous examples. I don't understand really why a language like rust with a decent package manager could not achieve what you have described. You have listed so much it's hard to answer. But I could say this, where there's a package manager there's probably a solution in ANY language. I'm glad you like PHP but keep an open mind, PHP is not forever π
That doesn't mean anything and is incorrect. Sure, it is Turing complete but so is every programming language that is widely used. That says nothing about its power, features or sanity.
There are intractable problems that can not be solved mathematically(thus can not be done using a programming language - programming is math, this is a fundamental misundering amateurs such as all PHP programmers fail to grasp) therefore PHP can not do everything. No language can.
Still waiting for the well-educated PHP fan...
Of course you are not going to be able to write an A.I program. No language can. All they can do is imitate intelligence by predicting stuff. But i dare you to try and do all that with any other framework you can think of without using PHP at least as an ajax receiver for DB querying efficiently and above all fast enough to do it all in one sitting.
All i am saying is, any idea that i had i was able to visualize and implement it with PHP much faster and more efficiently than any other language out there.
I've written custom web applications, web-to-game server interactions using sockets and db queries, very 'intelligent' parsers of all kinds, prediction algorithms, home automation scripts, obfuscators...you name it. The list goes on and on. PHP is used by 78.9% of all websites out there. You can easily hook up html, js, sockets or anything else you want into it really fast and test things out in a matter of seconds...when everyone else is still compiling.
You said "You can literally do anything you can think of with it." That is so wrong it is simply disproven and so shocking that only a PHP fan would write that.
Actually, AI is not on the list of intractable problems, if it were there would not be billions of dollars being dumped into it.
A basic and simple intractable problem is the Halting Problem, even a PHP fan might be able to understand that one.
That stat is flat-out wrong. Those stats will count a server that might have mod-php installed even if PHP is not running anything on it. Even if your laughable stats were correct, it would not prove anything other than there are a lot of morons out there, thankfully your stats are wrong.
I bet you consider Facebook to be a "PHP site" when all it is used for is template generation, none of the heavy work is done with it. Languages professionally designed(PHP is not designed in any manner whatsoever) languages is what runs the important backend stuff.
Using PHP has cost FB tens of millions of dollars and set them back years, that is probably a good thing since they are such a danger to the world.
PHP doesn't run anything important on the web. Not one thing.
PHP is dog crap, there is no technical merit, not even its fanboys can name anything technical that is good about.
Too bad PHP development can not even imitate intelligence.
This whole thread is crazzy π€£.
That people keep referring to the 'Fractal of bad Design' article which is from 2012 (!!) is tiresome.
Like 7 years of evolution doesn't deserve a new look? Sigh.
A few things are fixed, many problems added to that list. So the bottom line is his huge list of mind-melting problems is bigger today than in 2012.
The fact that PHP 5 is still used for new monstrosities also make the Fractal article relevant today.
So no it does not.
I fail to see how it's the fault of the language... that people & irresponsible hosting companies keep using the old versions.
It makes the article relevant.
I generally agree with the summary even though the points are not entirely consistent and it severely misses the big picture of "sucking".
Even though the strictly technical criticism of the cited article no longer applies to newer PHP, PHP as a language and ecosystem sucks a lot largely due to its maintainers but also the community.
PHP 5 had several problems that are at the root of that:
You offer some advice for PHP5 users and you also mention using strict types. Do you know that strict types are a PHP7 thing, right? You're basically telling people to upgrade, which comes with a learning curve, particularly for OOP practices. It's a costly upgrade and factor in that of all PHP applications in the wild almost 60% use version 5, people's opinion of PHP will stick to version 5. No wonder people still refer to that article!!
Also .... you tell people to follow PHP-FIG. Do you mean all standards? (that would be nice) or you just mean coding standards (aka PSR-2 - which incidentally was deprecated, but people still follow it and quote it out of inertia). I'm asking because one of the most famous frameworks (Symfony) isn't compatible with the most needed standards: PSR7 and PSR15. It's mind-blowing that incredibly useful components like HttpFoundation (I love working with their requests and responses) don't provide builders for PSR7 and PSR15. Yeah, they provide a bridge as a separate package which only adds to boilerplate.
Speaking of composer, I wouldn't be so eager to jump into packages. My usual tooling for APIs involves PHPDI's container, theleague's router and stuff like Monolog and Doctrine but that's about it. A recent nightmare involved mailgun and some sms sending API that each had the same dependency locked at different versions. It's a matter of time before running into version conflicts that, in the happy occasion when there's a resolution, ends up with using the lower common acceptable version (which, given the way most libraries go for compatibility, means code from PHP5 era - a great example for the developers that end up using it). In the case of node, for example, older packages aren't that problematic since even what's considered older at the moment (think node 6) isn't as old as vanilla PHP5.
And last, my personal gripe with the PHP ecosystem is that in many cases the tendency is to copy Java. Java-fication is most obvious on frameworks like the defunct Silex or Symfony. Fortunately now there are some nice alternatives (Symlex, Ubiquity) that are embracing PHP7 for what it is, instead of trying to copy another language. Add to that application engines like Roadrunner that bring true performance and nowadays we have something workable.
But there's also the tendency to get stuck in OOP world (I get it, PHP has be so slow to evolve that I've seen developers jump at OOP as if they suddenly discovered sliced bread - most OOP 'benefits' are a myth and while it did help promote great practices like SOLID, they aren't tied to OOP and apply regardless of core paradigm). Other languages offer functional programming alternatives or strong code support for it, PHP only got Swoft framework fairly recently and it's quite in its infancy.
I think you started with a great post but deviated in the middle of it.
If you step back for a second you can see the irony in the post.
You're asking people that hold strong opinions against PHP to open their minds a little and see the evolution of the language. The key component here is "strong opinion". It is the core of all these discussions.
Your article is packed with strong opinions such as "you shouldn't use a web framework", "you should use strict typing", and "active record is evil".
At the expense of trying to convince strong opinionated people that PHP dont suck you end up spreading your strong opinion against one of the largest PHP community: Laravel. A web framework without static typing build with Active Record in mind.
It makes your article read as: "Hey people, you have some pretty bad strong opinion about PHP. Here are some up to date strong opinions you should hold so that we can collectively hate on other communities instead".
My reply may seem like I have some strong strong opinions of my own and to a certain extent that is true, but what I've been learning is that it's best to keep ripping strong opinions apart and being accepting of the diversity. As an example, you could have said that with PHP you're not forced into a web framework if you dont like to be bound to one, but for those that do like it we have some great ones like Symfony, Laravel and Slim.
TL;DR: Try not to advertise against strong opinions with more strong opinions.
I understand what you mean by that. And yes, I do hold strong opinions on my own. But the fact is, I'm not forcing any of those ideas to anyone.
Strict typing is optional, as I mentioned in the post, in a dynamically typed language of course you'd like to use the dynamic typing. What I wanted to convey is: use dynamic typing when you need to, otherwise stay on the safer side with predictability.
Also, I am not targeting any particular framework. Laravel has its problems and so does every other framework. Choose what you need to, when you need to. I personally like taking pieces off Symfony and wiring them up together. I never said, "never use a web framework". I recommended to not get tightly coupled to one framework.
About Active Record: yes, I do believe it's an Anti-pattern. And I would advise anyone against it.
Not all programming languages suck. If someone is not comfortable with the headaches of programming, it's not that programming sucks.
I can for sure say PHP programming is more joyful than many of others.
Most of the times I see someone blames PHP, s/he is less familiar with PHP and in fact, is angry of why PHP makes more sense than the language s/he code in.
I use it from times to times. It's just a good programming lang for me...
Thanks for sharing the links that you did. I got to read the amazing "PHP: a fractal of bad design" and saw the people who build PHP itself. I am a MERN stack developer and I started to explore PHP and WordPress theme development.
Ooh boy, the ecosystem, design principle in PHP and WordPress are just so bad! They are extremely bad. I'm not saying this because I have anything against PHP, I willingly came to learn it with an open mind and I will keep studying it.
But as a PHP developer now, I must say, PHP is a badly designed language and things like WordPress complement bad design further.
Putting people on blast for making a blanket put down on PHP while doing the exact same thing to JavaScript deflates your points.
I am not entirely against Javascript. I use Javascript because I need to, not a fan of how it does things but I need it.
It's a thing that grew out of experience, as I mentioned - some people tried to shove it down my throat by talking of its superiority over everything else. Then I realized what the community is doing.
Every year a new cool Javascript framework/library comes out, and everyone - especially the newbies run towards them while making it a big fuss about it and try to push everyone to that as well.
Node is super fast, use it everywhere!
I don't see that happening at the PHP community, or anywhere else to be honest.
PHP is meant for the web, and it only does web and does it well. Javascript on the other hand... browser -> server -> desktop apps... I just hope it doesn't end up in embedded systems.
Well, that last bit is not true. You can use PHP for more than just Web.
As for the other part, well, you can call them noobies if it makes you feel better, but for someone who has been doing this for more than 20 years, all developers who complain about one framework or the other look like noobs. All frameworks have their pros and cons, and the best point made in your post is that the cons almost always come from people who don't know what they are doing, but that point hurts you when you start complaining about Node because well... do the math.
Again, I'm not complaining about Node. I'm complaining about the people who constantly keep pushing Node to others.
The difference lies in the fact that I'm not trying to force my opinion on anyone about JavaScript. People who keep pushing JS everywhere do.
I believe JS does fine for client side, that's what it was meant for. But the community started trying to bring it to everything else too. And the newcomers somehow fall into the trap of blindly following any cool new framework that comes their way and they keep pushing it everywhere.
I'm sure more experienced developers like yourself understand that languages are just tools and some tools are more fit for some things better than other.
And yes, you can use PHP for other things too. But you wouldn't do it in production in your right state of mind.
Node works fine in other areas, too. It's got plenty of things I dislike and wouldn't be my first go-to in most cases, but implying that it doesn't belong in those areas is the same backwards thinking as "PHP is trash".
My point is, you have kind of a roundabout way of putting down the other guy in the same way that you've probably been put down, and most likely, neither really deserve it.
As for that production comment, I'm going to disagree with you again. Consider if you have a framework that includes a PHP library that is the center for managing certain entity types. That functionality may include file i/o, database ops, external API integrations, etc., now consider your CI needs to perform some manipulations in certain workflows, would it be smarter to rewrite that functionality in another language or use PHP and import the library you already have?
If you complain about using a framework with Python and then happily write you use composer and that Doctrine is good then no one should take you seriously.
"Your coworkers are probably too free, maybe their code is still compiling."
You realize most Go applications are small and compile within 3 seconds, right?
Your arguments are just invalid. Active record is not evil. The thing that is evil here is Doctrine with the insane amount of abstraction which makes doing things efficiently impossible.
Doctrine is a great example of what is wrong with PHP and the people who invented it didn't help the cause. It makes it slow down to a halt with inefficient queries and cluttered classes.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.