PHP sucks, can it suck less?
Tags: php, javascript, oop, webdev
Published: 2019-09-26 18:39:33


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](https://www.twodee.me/blog). 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](https://eev.ee/blog/2012/04/09/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](https://en.wikipedia.org/wiki/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](https://getcomposer.org/) 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.