Building proxies, decorators and delegates in PHP5

July 3rd, 2006 by Ivo

In PHP5, you can define methods in an object for intercepting calls to methods of an object and for intercepting access to object member variables. These methods (__get, __set and __call) enable the implementation of very generic proxies, decorators and delegators.

(For those unfamiliar with these design patterns, the extensive descriptions at wikipedia are worth the read.)

What proxies, decorators and delegates have in common is that they all change a part of the behaviour of an underlying object by overriding its methods, while forwarding the rest of the calls to the original object.

I wouldn't like this post to be purely theoretical, so let's use a simple example with decorators to demonstrate the concept.

We would also like to create classes that decorate the hello-saying. I want to have a decorator that makes the output bold, and another one that makes the output italic. (You could create a class that extends HelloWorld, but more in this later. For the moment, assume that inheritance is not an option.)

In classic code, if I wanted to decorate the sayHello method, I could create a decorator object that looks like this:

This would work like a charm, but the problem is in the doSomethingElse method. Well, not really a problem, it's just that I have to redefine it to forward the call to the original object. What if we had 5 methods, or 10? All of them would need to be forwarded.

What if there are member variables in place that should be accessible? You'd have to forward those too. (I know, exposing member variables is Evil, but sometimes the object in question is not developed by yourself and you just have to deal with it.)

PHP5 has excellent ways to solve this, and fairly easy too. It allows us to build a generic base class for our decorators that takes care of the whole forwarding problem. (Credit for this class goes to my coworkers Martin and Peter).

Notice how we no longer defined the doSomethingElse method? It is automatically forwarded to the original object. In fact, an object of type ItalicHelloWorld will behave exactly like a HelloWorld object. Any caller wouldn't know the difference. Except ofcourse that sayHello returns a manipulated result.

I promised to explain why inheritance wouldn't be the solution here. By providing both an ItalicHelloWorld and a BoldHelloWorld, I've tried to demonstrate this. What if you needed a string that was both bold and italic? Which of the two would you extend? PHP doesn't have multiple inheritance, so we would be stuck there.

Using this approach, we've worked around the need to have multiple inheritance.

This technique is also useful if you are a fan of Aspect Oriented Programming. You could have an object that implements a certain aspect and applies that to the underlying object. Instead of overriding a single method, you can override __call and implement an aspect.

Let's take security as an example. Suppose we have an object, but we want to secure access to any of its members. We could use the following class:

Another aspect that can easily be implemented like this is caching of resource intensive methods. Just add a list of methods to cache, and a member variable to hold results for cached methods.

With this post I've tried to demonstrate the power of PHP5's __call, __get and __set special functions. Java has similar solutions using reflection, but PHP5 makes it much more easy to create constructs like this. This also makes it possible to easily create patterns with small amounts of code.

Feel free to use the code samples in this post. You can link back to this post from your code to explain how the code works, if you like.

That is indeed correct, this would make the interface the same. However, in your solution, the ItalicHelloWorld would have its own state, meaning that doSomethingElse() would work on member variables of the ItalicHelloWorld instance and not on member variables of HelloWorld instance. (the difference between $this->m_member and $this->m_object->m_member)

Unless you reimplement all methods, which is the point of this article; using the AutoForward baseclass, you won’t have to do that.

Also have a look at the security example near the end. It is applicable to virtually any class, which wouldn’t be possible if SecurityAspect would inherit from one of them.

This can be worked around very easily by making the base class and all of it’s decorators extend a parent interface. Using __call does mean that the interface will probably have to be empty. (That is unless you create a seperate interface for each method/group of methods that you anticipate decorating. Which gets cumbersome.)

July 26, 2006 at 4:51 pm, paul said:

While I think that proxying functions from more then one base object in one environment IS VERY useful when dealing with shared environments I beg you specify this explicitly. IE: This is a shared environment programming pattern do not use it to draw circles…

Can’t really se any true and undenyable reason to use these techniques while not invoking the all-to-well-known -”not my code, have to go round” reason, which is by the way just a sign of very poor analysis and granulation skills.

Other then that nice tute. – and I like the oracle halo of the company