PHP Advent Calendar Day 9

09 Dec 2007

Today's entry, provided by Ivo Jansch, is entitled Design Patterns.

Ivo Jansch

Name
Ivo Jansch
Blog
jansch.nl
Biography
Ivo Jansch is CTO of Ibuildings, a UK and Netherlands based PHP service company. Ivo is an active blogger in the PHP community, does occasional consultancy work for Zend Technologies, and is the author of the business framework ATK.
Location
Netherlands

In my work, I visit a lot of companies that work with PHP, and I get to talk to a lot of PHP developers. I have noticed that, although most of them are familiar with object-oriented programming, the concept of Design Patterns is often new to them.

Design patterns are best practice solutions for common programming challenges. Sometimes, people confuse them with a library, or snippets of code, but they're more a blueprint for code than an actual piece of code. This way, the solution is independent of the language you're working in. Most design patterns can be implemented in any object-oriented language.

Imagine you are working on part of an application, and you need to make sure that a particular object you created is instantiated only once. If other developers are using your classes, you don't want them to create multiple objects. For example, your class works with an external resource that only accepts a single connection, and your class regulates this, so you can't have multiple instances of your class connecting to this resource.

The solution to this problem is the singleton pattern. This pattern is one of the most common design patterns and is fairly easy to implement. The singleton pattern is based on the following solution:

See how this is just a simple specification with no code?

The following is an example implementation in PHP:

  1. <?php
  2.  
  3. class MyObject
  4. {
  5.     private static $_instance = NULL;
  6.  
  7.     private function __construct()
  8.     {
  9.     }
  10.  
  11.     public static function getInstance()
  12.     {
  13.         if (self::$_instance == NULL) {
  14.             self::$_instance = new MyObject();
  15.         }
  16.  
  17.         return self::$_instance;
  18.     }
  19. }
  20.  
  21. $obj = MyObject::getInstance();
  22.  
  23. ?>

In other languages, the result is similar.

The singleton pattern is probably the easiest design pattern, but it is a good example of how a design pattern is just a common solution to a common problem.

There are many more design patterns, such as an iterators to generically loop things, proxies to proxy access to particular classes, decorators to add functionality to a class without changing the class its itself, and the MVC pattern, a popular pattern for web development. The MVC pattern provides a clean solution for separating business logic (model), layout (view), and application control logic (controller).

The MVC pattern is a popular pattern used by many frameworks, such as the Zend Framework. Creating applications using this pattern can make them easier to understand and easier to maintain.

If you want to take advantage of design patterns, how can you possibly know which patterns exist and which problems they solve? An important resource is the book that started it all, Design Patterns: Elements of Reusable Object-Oriented Software by the "Gang of Four." This book, which is used in most computer science courses, explains the basics as well as most of the patterns.

There's another book that's more relevant to PHP developers, php|architect's Guide to PHP Design Patterns by Jason Sweat. Not all patterns that exist are useful when programming PHP. In this book, Jason discusses the ones that are relevant to PHP and gives examples that implement them.

A final resource for design patterns is Wikipedia. It contains very extensive documentation on patterns, and for some it provides implementation examples in several languages, including PHP.

If you have a large code base and are looking for ways to improve the maintainability of your code, have a look at design patterns. They can be very powerful.