Why I love PHP & How to write good PHP code

© Andrew Chapman (The PHP Ninja) 2008

Andrew Chapman is a PHP Contractor / Freelancer based in Melbourne Australia.

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 it's 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 unnecessary so that the necessary may speak."
Hans Hofmann (A German who could paint rather well).

"Everything should be made as simple as possible, but not simpler."
Albert Einstein (Another German - I'm sensing a trend).

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 tell force you into programming in a certain way and it let's you choose whatever IDE, operating system and architecture your heart desires. I recently danced with ASP.Net and quite honestly I hated it. Whilst C# is a very expressive language, ASP.Net tries to re-educate you on how to build a web page. For example, you are encouraged to use "master pages" instead of CSS to alter the presentation of a page. Moreover, Microsoft has tried to bring the desktop application style "event" architecture to the web; often this simply doesn't correlate to how a web page truly works and it's ugly.

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 PHP classes written and released for free on places like 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. However, if you build a structured and well engineered framework around PHP's simplicity, what you end up with is simplicity that "just works" and is highly maintainable and easily understood. Several excellent frameworks have been designed by the PHP community and released for free, such as CakePHP, CodeIgniter, Smarty and the Zend framework.

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's some important facts about PHP:

Contact the PHP Ninja

Click here to go back to the front page.