Design Patterns in PHP5

With the release of version 5 PHP has really come of age, it had already conquered the home hacker market where the primary focus was on making it work, rather than making it work well. This was all well and good but PHP had always had trouble gaining ground in the smaller (in numerical terms), but far more profitable (in monetary terms) arena of enterprise level applications.

The elements of PHP that traditionally attracted the home hacker projects to the language, such as its forgiving nature and the ability to solve the same problem in half a dozen ways, were the exact same elements that drove away enterprise level projects. Enterprise level projects are far more interested in maintainability and scalability than hosting costs or the number of tutorials to be found online.

Despite it being possible to write good code using OOP in PHP4 - just look at PEAR, it was far from being a pleasant experience and always felt like it was more of a hack than a real solution. That’s not surprising as implementation of OOP in PHP4 was basically a series of hacks and work-rounds. Luckily the open source community is a stubborn and persistent bunch and continued to build solid LAMP (Linux Apache MySQL PHP) based apps until PHP got the upgrade it so desperately needed.

Now PHP5 has everything you need to write solid code using OOP, including access modifiers and the built in constructor/destructor methods that were so sorely missing in PHP4. To check out all of the new stuff in PHP5 here’s a break down.  

The best part about the new functionality in PHP5 is that it really opens the door to the world of design patterns for PHP. Almost all problems you encounter when programming have been solved hundreds of times before you ever even realized the problem existed. Design patterns are a cross language solution to everyday challenges faced by programmers. Using OOP and design patterns in conjunction will allow you to write solid reusable code which will make you a more efficient programmer. Remember programming is about working smart, not hard.

For a tutorial in the five most common design patterns check out this article from IBM. The code examples are all written in PHP5 but as with all design patterns will port easily to other OOP compliant languages.

If that tutorial wets your whistle, then you should go out and get yourself a copy of PHP: Objects, Patterns and Practice by Matt Zandstra of Yahoo UK and published by Apress. It can be purchased here as an ebook from the Apress website.