<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jansch.nl &#187; proxy</title>
	<atom:link href="http://www.jansch.nl/tag/proxy/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jansch.nl</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Sun, 31 Jul 2011 14:58:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Building proxies, decorators and delegates in PHP5</title>
		<link>http://www.jansch.nl/2006/07/03/building-proxies-decorators-and-delegates-in-php5/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-proxies-decorators-and-delegates-in-php5</link>
		<comments>http://www.jansch.nl/2006/07/03/building-proxies-decorators-and-delegates-in-php5/#comments</comments>
		<pubDate>Mon, 03 Jul 2006 19:46:22 +0000</pubDate>
		<dc:creator>Ivo</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[decorator]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[magic methods]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://www.jansch.nl/?p=441</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://en.wikipedia.org/wiki/Proxy_pattern">proxies</a>, <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorators</a> and <a href="http://en.wikipedia.org/wiki/Delegation_pattern">delegators</a>.</p>
<p>(For those unfamiliar with these design patterns, the extensive descriptions at <a href="http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29">wikipedia</a> are worth the read.)</p>
<p>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.</p>
<p>I wouldn't like this post to be purely theoretical, so let's use a simple example with decorators to demonstrate the concept.</p>
<p>Suppose we have the following class:</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> HelloWorld
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Hello World&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> doSomethingElse<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #808080; font-style: italic;">// &quot;Hello World&quot;</span></pre>
<p>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.)</p>
<p>In classic code, if I wanted to decorate the sayHello method, I could create a decorator object that looks like this:</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BoldHelloWorld
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$m_object</span> = <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> BoldHelloWorld<span style="color: #66cc66;">&#40;</span>&amp;<span style="color: #0000ff;">$object</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span> = <span style="color: #0000ff;">$object</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;strong&gt;&quot;</span>.<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;&lt;/strong&gt;&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> doSomethingElse<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>-&gt;<span style="color: #006600;">doSomethingElse</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">new</span> BoldHelloWorld<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// &quot;&lt;strong&gt;HelloWorld&lt;/strong&gt;&quot;</span></pre>
<p>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.</p>
<p>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.)</p>
<p>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).</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/**
  * AutoForward baseclass for automatic forwarding of
  * method calls and member variables.
  *
  * @author Peter C. Verhage
  * @author Martin Roest
  */</span>
<span style="color: #000000; font-weight: bold;">class</span> AutoForward
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0000ff;">$m_object</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">/**
   * Constructor.
   *
   * @param Object $object
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span>&amp;<span style="color: #0000ff;">$object</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span> = <span style="color: #0000ff;">$object</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/**
   * Returns the forwarded object.
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> &amp;__getObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/**
   * Forward method calls.
   *
   * @param String $method method name
   * @param Array $args method arguments
   * @return Unknown method return value
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$method</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">return</span> <a href="http://www.php.net/call_user_func_array"><span style="color: #000066;">call_user_func_array</span></a><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>, <span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/**
   * Forward property set.
   *
   * @param String $name property name
   * @param Unknown $value property value
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span>, <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>-&gt;<span style="color: #0000ff;">$name</span> = <span style="color: #0000ff;">$value</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/**
   * Forward property get.
   *
   * @param String $name, property name
   * @return Unknown
   */</span>
  <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>-&gt;<span style="color: #0000ff;">$name</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>By overriding __set, __get and __call, any methods to an object of the AutoForward class are automatically forwarded to the contained object.</p>
<p>Let's use this class to create our bold and italic decorators:</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BoldHelloWorld <span style="color: #000000; font-weight: bold;">extends</span> AutoForward
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;strong&gt;&quot;</span>.<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;&lt;/strong&gt;&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ItalicHelloWorld <span style="color: #000000; font-weight: bold;">extends</span> AutoForward
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> sayHello<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;&lt;em&gt;&quot;</span>.<span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">&quot;&lt;/em&gt;&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">new</span> ItalicHelloWorld<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// &quot;&lt;em&gt;Hello World&lt;/em&gt;&quot;</span></pre>
<p>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 <strong>exactly</strong> like a HelloWorld object. Any caller wouldn't know the difference. Except ofcourse that sayHello returns a manipulated result.</p>
<p>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.</p>
<p>But not in our example. It would simply be a matter of:</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">new</span> BoldHelloWorld<span style="color: #66cc66;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> ItalicHelloWorld<span style="color: #66cc66;">&#40;</span>
<span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// &quot;&lt;strong&gt;&lt;em&gt;Hello World&lt;/em&gt;&lt;/strong&gt;&quot;</span></pre>
<p>Using this approach, we've worked around the need to have multiple inheritance.</p>
<p>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.</p>
<p>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:</p>
<pre class="php">&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SecurityAspect <span style="color: #000000; font-weight: bold;">extends</span> AutoForward
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$method</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>isAllowed<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">return</span> <a href="http://www.php.net/call_user_func_array"><span style="color: #000066;">call_user_func_array</span></a><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">m_object</span>, <span style="color: #0000ff;">$method</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$args</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Caller not allowed to execute $method on this object.&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$obj</span> = <span style="color: #000000; font-weight: bold;">new</span> SecurityAspect<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">sayHello</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// throws error if sayHello not allowed</span></pre>
<p>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.</p>
<p>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.</p>
<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jansch.nl/2006/07/03/building-proxies-decorators-and-delegates-in-php5/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

