About the Author

Chris Shiflett

Hi, I’m Chris: entrepreneur, community leader, husband, and father. I live and work in Boulder, CO.


PHP Advent Calendar Day 9

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:

  • Make the constructor of the class private, so nobody can call it, except the class itself.

  • Create a static method that returns the one and only instance of the object. If there is no instance yet, create it when the first call to this method is made. This method is usually called getInstance().

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

The following is an example implementation in PHP:

<?php
 
class MyObject
{
    private static $_instance = NULL;
 
    private function __construct()
    {
    }
 
    public static function getInstance()
    {
        if (self::$_instance == NULL) {
            self::$_instance = new MyObject();
        }
 
        return self::$_instance;
    }
}
 
$obj = MyObject::getInstance();
 
?>

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.

About this post

PHP Advent Calendar Day 9 was posted on Sun, 09 Dec 2007. If you liked it, follow me on Twitter or share:

3 comments

1.Sebastian Bergmann said:

private final function __clone() {} is also required. Otherwise you could clone an object that is supposed to be a Singleton.

Mon, 10 Dec 2007 at 12:23:30 GMT Link


2.Stan said:

"Design Patterns" by the "Gang of Four" is quite good, but it's also quite dated. I believe it was printed in 94, so it's missed some of the patterns which have emerged because of web applications, as well as some of the more recent patterns in general. While I would never discourage anyone from that text, if you're looking for a good patterns book I would highly recommend Martin Fowler's "Patterns of Enterprise Application Architecture". Fowler is genius, and this book addresses many of dated issues in the "Gang of Four's" text. Additionally, Fowler tries to include some of the web-specific design patterns that have emerged more recently. Lastly, as an additional resource take a look at www.phppatterns.com while this is far from complete it does attempt to give some specific examples in PHP for PHP developers.

Thank you Ivo for your article, it is much appreciated!

Pax.

Mon, 10 Dec 2007 at 14:19:48 GMT Link


3.Stan said:

Sebastian,

Good point, also note that in many cases if not made private clone can additionally be manipulated to simulate the singleton constructor as an alternative.

Pax,

- Stan

Mon, 10 Dec 2007 at 14:23:57 GMT Link


Hello! What’s your name?

Want to comment? Please connect with Twitter to join the discussion.