Why I love PHP & how to write good PHP code.
© Andrew Chapman (The PHP Ninja).
Andrew is a PHP Contractor / Freelancer based in Oxford, Oxfordshire.
It seems like every day there’s a new article from either the .Net or the Ruby on Rails camp about how wonderful their environments are, and how PHP is a language for “the lowest common denominator”. Essentially the proponents of this school of thought are saying that PHP is so simplistic and “dumbed down” that it’s only used by idiots. To a certain extent I agree, PHP is a very simple language and its semantics can be easily learned. However, saying that something is rubbish because it’s simple is foolish. Simplicity is something we should strive for in everything we do. To quote some smart people:
“The ability to simplify means to eliminate the unnecessaryHans Hofmann (a German who could paint rather well)
so that the necessary may speak.”
“Everything should be made as simple as possible,Albert Einstein (another German – I’m sensing a trend)
but not simpler.”
PHP does eliminate all of the unnecessary things that can make programming long and tedious. There are no headaches due to memory allocation or pointers (like in C/C++), there are few data type declarations to worry about, and the language was built from day one to make building web pages as simple as possible. The sheer richness of the in-built PHP library makes common tasks like accessing databases, reading and writing XML, communicating with web services and creating PDF documents so easy that it’s criminal.
Another important thing about PHP is the freedom that it gives you: PHP does not force you into programming in a certain way and it lets you choose whatever IDE, operating system and architecture your heart desires.
In the dark ages before PHP V5 was released, writing good object oriented code in PHP was difficult if not impossible due to the limitations of the language. For example, PHP V4 had no private/protected/public data hiding declarations, had no support for abstract classes or interfaces, and had no knowledge of static and final methods. However, these days PHP supports all of the important object oriented paradigms and there has been a wealth of diverse frameworks and classes written and released for free on places like GITHub, SourceForge and phpclasses.org. If you want a solution for a given problem, type it into Google and odds are good that the code has already been written for you.
It’s true that PHP’s simplicity makes writing bad code easy: in the hands of a novice you can end up with nasty spaghetti code that is difficult to read and full of security holes. However, if you build a structured and well engineered framework around PHP’s simplicity, or use one of the many existing great PHP frameworks like Laravel, Phalcon, Zend or CodeIgniter, what you end up with is simplicity that “just works” and is highly maintainable and easily understood.
Writing good PHP code
Writing good PHP code requires discipline; it’s often tempting to write things the quickest way without giving much thought to extensibility and maintainability; this is a mistake, especially for larger projects! If you take the time to build reusable objects that are well commented and consistent in design, development will get much faster later on. Moreover, if you test each object thoroughly as you go, you will have fewer bugs and a far easier maintenance job. Developers who work with your code down the track will thank you!
Finally, in order to write good PHP code it’s worth knowing the most common design patterns, like ”Model View Controller“ (used to separate display logic from data and control logic), ”Adapter“ (used to provide a common interface to other incompatible interfaces), and “Active Record“ (used to provide a read/write access to a single database row). The whole point of design patterns is to provide tried and tested solutions to common problems in software design – they are about not re–inventing the wheel and using what is known to work well. I’ll write an article about design patterns soon.
That’s all for now, but here are some important facts about PHP:
- PHP is used by over 244 million web sites and 2.1 million servers (www.php.net/usage.php).
- PHP supports both procedural programming and object oriented programming.
- PHP is amazingly flexible and allows you do crazy but useful things like passing functions as parameters and invoking object methods named dynamically.
- PHP is completely free – you do not have to pay anything to use it and the source code is always available. Any developer can extend the language if required.
- PHP runs on all popular operating systems and web servers.
- PHP has been around for over 20 years and has been consistently supported, extended and enhanced.
- Rasmus Lerdorf wrote the original version of PHP in 1995 – for a full history click here.
Contact the PHP Ninja
Call me on 07943 651675 or email me at firstname.lastname@example.org.