No more object-oriented programming with PHP

Published on 2021-03-27. Modified on 2021-07-19.

It has happened again. Another company has contacted me because they had a PHP product developed which has turned into a huge beast of complexity. Basic functionality has stopped working and nobody wants to have anything to do with it. It has serious consequences for the company because they are loosing money since the product isn't up and running. I have come to the same conclusion which I have reached in other similar situations, the codebase is one big pile of complex object-oriented mess.

A problem nowadays is that too much PHP software is being developed with all the object-oriented design principles and all the design patterns theory in focus. It is then combined with some front controller junk framework, which adds an unnecessary layer of complexity on top of the build-in controller of the web server. The problem that the software is supposed to solve takes a backseat to all this rubbish.

While I certainly can see and do value some of the benefits that object-oriented theory brings to the table, it's usefulness is highly overrated and I believe that a lot of the concepts adds a lot of unnecessary complexity.

Also see my other article: Go - understanding the object-oriented features with structs, methods, and interfaces

I think what happened in PHP land, and also in other parts of the industry, was that a lot of people back in the days wrote really horrible procedural code, really bad spaghetti code. PHP was very easy to learn and suddenly everyone was making horribly insecure and poorly designed web applications. When the concepts of object-oriented programming was introduced into PHP it presented a new way of organizing code which helped people improve the code structure. But then came the "corporate hype machine"! It blew everything totally out of proportions and even the academic circles got infused with this new mental model, that procedural programming is messy and outdated and object-oriented programming is the new true way to software development.

Joe Armstrong, the inventor of Erlang, says it well his famous article Why OO sucks:

If a language technology is so bad that it creates a new industry to solve problems of its own making then it must be a good idea for the guys who want to make money. This is is the real driving force behind OOPs.

Code should always be readable and well structured, it doesn't matter what paradigm we're dealing with, but code needs to fit in your brain, not in an artificial structure confined and limited by theories that never truly worked.

The Linux kernel, the BSD operating systems, all these projects are done in procedural C. These projects are huge in size, yet most of the coders that work on these projects use simple console based text editors like Vim. They do not depend on modern IDEs to help them navigate the code.

I have also noticed a pattern. When working in projects with a procedural programming language people focus completely on the problems. But, when a project is done in an object-oriented programming language, the development slows a lot and people succumb to the theories of abstraction, encapsulation, and all the complexities of shared mutable objects and methods, etc. Too much time is wasted thinking about design patterns and all the other object-oriented dreck instead of doing actual work.

When we design and develop software, we need to write code that is easy to understand, code that is efficient and performant - especially nowadays - and code that is reliable. And the very best way to do that is to reduce code complexity. And absolutely nothing adds to code complexity as object-oriented concepts and implementations.

Frantic OOP people reading this article will complain:

Sigh!

If you stick to good old and efficient procedural programming and combine that with the strict typing in PHP, and you avoid the well known and common problems with PHP and bad coding practices, you can become much more productive very fast because you no longer need to think about all the added complexity of object-oriented programming. You will be focused on solving problems!

Some of the advantages of procedural programming is that the programs are straight forward with precise usage. The code is compact and reusable and it breaks problems into smaller pieces that are handled in an order that fits how the computer works. It also utilize computer resources effectively.

And remember, just because something gets stuffed into a class doesn't make it object-oriented. You can fully utilize PHP's autoloading feature, if you feel you need that, without using any object-oriented programming at all.

Related reading