<?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>ActionScript 3.0 Design Patterns &#187; PHP</title>
	<atom:link href="http://www.as3dp.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP Chain of Responsibility Design Pattern from ActionScript 3.0</title>
		<link>http://www.as3dp.com/2009/11/php-chain-of-responsibility-design-pattern-from-actionscript-30/</link>
		<comments>http://www.as3dp.com/2009/11/php-chain-of-responsibility-design-pattern-from-actionscript-30/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 00:13:56 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2131</guid>
		<description><![CDATA[ARE Design Patterns portable from one language to another? Of course they are. I&#8217;ve read some accounts that claim otherwise, but I&#8217;ve not found any design pattern that could not be used in any computer language that I&#8217;ve tried. (Maybe I&#8217;m just conversant in languages where design patterns do work, but I don&#8217;t think so.) [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/11/smallchain.png" alt="smallchain" title="smallchain" width="240" height="90" class="alignleft size-full wp-image-2165" />ARE Design Patterns portable from one language to another? Of course they are. I&#8217;ve read some accounts that claim otherwise, but I&#8217;ve not found any design pattern that could not be used in any computer language that I&#8217;ve tried. (Maybe I&#8217;m just conversant in languages where design patterns do work, but I don&#8217;t think so.) Anyway, Chandima and I have been discussing doing a set of PHP design patterns, and I wanted one for a contact form I had done using PHP.</p>
<p>For this blog, though, I thought I&#8217;d use a translation from one of the design patterns we have on this blog so that you can more easily see the similarities and differences between  ActionScript 3.0 and PHP  in implementing the same design pattern.</p>
<p><strong>Chain of Responsibility</strong></p>
<p>As you may recall from our <a href="http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/">Chain Of Responsibility (CoR) post</a>, the design pattern is used when you want to have a system that takes care of requests when you have different outcomes.  In our original example, we added a helper class called Request, but otherwise we stuck with the basics of the CoR structure. Figure 1 shows the class diagram of the CoR we used in our initial post.<br />
<div id="attachment_2135" class="wp-caption alignnone" style="width: 499px"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/phpcordia.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Chain of Responsibility Class Diagram&lt;/em&gt;" title="phpcordia" width="489" height="253" class="size-full wp-image-2135" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Chain of Responsibility Class Diagram</em></p></div></p>
<p>In this particular design pattern the Client class <em>is</em> a full-fledged participant in the pattern. Because <strong>Client</strong> classes are <em>request objects</em> themselves, it  may seem odd to have a <strong>Request</strong> class. Just think of the Request class as a helper class that encapsulates requests for the Client.<br />
<span id="more-2131"></span><br />
The back story to this CoR implementation is that you have a company that needs to distribute pick-up trucks using the minimum size truck for any given load. So, you set up a CoR design to check the load requirements sent in, and then dispatch the smallest possible truck for a given load. (For other details of the CoR and ActionScript 3.0, check out the original CoR post.)</p>
<p><strong>The PHP implementation of CoR</strong></p>
<p>Assuming you&#8217;ve looked at the original CoR post, let&#8217;s jump right in to the PHP implementation. Figure 1 shows the exact same set of classes that we&#8217;ll use with the PHP version of the design. We&#8217;ll start at the heart of the CoR with the <strong>Handler</strong> class. PHP has true <em>abstract classes</em> and we can create the Handler using an abstract class. (In case you&#8217;re wondering, we probably could have  done the same thing with an interface, but tweaking seems easier using abstract classes.)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2131code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p21314"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2131code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	abstract <span style="color: #000000; font-weight: bold;">class</span> Handler
	<span style="color: #009900;">&#123;</span>
		abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <strong>handleRequest()</strong> method uses an encapsulated request from the Request class that is instantiated in the Client. As you can see, PHP does not have strong typing; so the parameters are not typed. The setSuccessor() establishes the next link in the chain. If the current object (class) cannot provide what is requested, the request is passed to the successor.</p>
<p>For this example, three concrete Handler objects—GMC, Dodge and Ford—handle all requests. If a load is too heavy for the lightest truck, the request is passed on to the next one and so on until the request reaches the end of the chain. For this example, there&#8217;s no &#8220;too heavy&#8221; notice if the load surpasses the capacity of the largest truck, but it&#8217;d be easy to add such a notice or another class that provides remedies for loads too heavy for the largest truck. (In case you&#8217;re considering a solution, <em>think modulo</em>.)</p>
<p><strong>The Concrete Handlers</strong></p>
<p>The three concrete handlers represent the load capacities of three trucks. Each concrete handler looks at the load and if it can handle it, it says so. Otherwise, it passes the request up the line until it reaches the end of the chain or a truck with in the load parameters.  The following three classes represent concrete handlers:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2131code5'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p21315"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code" id="p2131code5"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//GMC Class</span>
	<span style="color: #000000; font-weight: bold;">class</span> GMC <span style="color: #000000; font-weight: bold;">extends</span> Handler
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$successor</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">=</span><span style="color: #000088;">$nextTruck</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1547</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A GMC Canyon would be good for this. Weight: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
//Dodge class
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">class</span> Dodge <span style="color: #000000; font-weight: bold;">extends</span> Handler
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$successor</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">=</span><span style="color: #000088;">$nextTruck</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1750</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A Dodge Dakota can handle this load. Weight: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
//Ford class
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">class</span> Ford <span style="color: #000000; font-weight: bold;">extends</span> Handler
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$successor</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">=</span><span style="color: #000088;">$nextTruck</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1890</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This load requires a Ford 150. Weight: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As you can see, each link in the chain (concrete Handler class) uses a simple conditional statement to handle the request. The &#8220;handling&#8221; is merely using an <strong>echo</strong> statement to announce that a particular truck can handle the load. (In this case the Ford may be hopelessly unrealistic, but never mind.)</p>
<p><strong>The Client and Request</strong></p>
<p>I always like it when the Client class is named as a participant in a design pattern, and with CoR, it is a main one directly tied into the Handler. Just the Client, Handler interface, and the Concrete Handler classes make up CoR. When using PHP, you need a calling file, and so an added file calls the Client. The Request object is actually one method smaller than the original Request created for ActionScript. As a getter/setter the setter is in the Constructor function, and so it seemed redundant to add a setter method in addition to the Constructor. The getter method encapsulates the value of the request generated originally in the Client. All three files are included in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2131code6'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p21316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code" id="p2131code6"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Request class</span>
<span style="color: #000000; font-weight: bold;">class</span> Request
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$weight</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">=</span><span style="color: #000088;">$weight</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPounds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Client class</span>
<span style="color: #000000; font-weight: bold;">class</span> Client
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$Acme</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GMC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Paramount</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dodge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Pinacle</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ford<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSuccessor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Paramount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Paramount</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSuccessor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Pinacle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Generate and process load requests</span>
		<span style="color: #000088;">$loadup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loadup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$loadup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1650</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loadup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$loadup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1795</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loadup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Testing file</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ERROR_REPORTING</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Client.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Handler.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Request.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GMC.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Dodge.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ford.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$testClient</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Client<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>When you test the program, you should see the following output:</p>
<p><strong>A GMC Canyon would be good for this. Weight: 1200<br />
A Dodge Dakota can handle this load. Weight: 1650<br />
This load requires a Ford 150. Weight: 1795</strong></p>
<p>I added the input values (weight) to show that this CoR knew what it was doing. Albeit, it&#8217;s not doing much other than demonstrating the CoR structure. However, I&#8217;ve got big plans for the CoR in PHP for automatically handling inquiries. The more options available, the more useful the CoR becomes. And of course, since it&#8217;s a design pattern, adding and changing options is quite easy.</p>
<p><strong>Some Differences</strong></p>
<p>The biggest differences in the programs are PHP and ActionScirpt 3.0 syntax differences. Here are some you can see immediately:</p>
<ul>
<li>PHP uses <strong>-></strong> instead of <strong>.</strong> for linking  properties and methods to an object (e.g., <strong>myObj->myProp</strong> instead of <strong>myObj.myProp</strong>)</li>
<li>PHP has no data typing</li>
<li>PHP requires <strong>include</strong> for all the class files in use (in the testing file)</li>
<li>All variables have dollar signs (<strong>$</strong>) in front of them</li>
<li>PHP concatenation uses a dot (<strong>.</strong>) instead of a plus sign(<strong>+</strong>)</li>
<li>PHP constructor functions use <strong>__construct()</strong> instead of the class name</li>
</ul>
<p>Other kinds of differences are the  idiosyncrasies of each language, and you may have to push and pull a bit when applying design patterns of one to the other. However, the nice thing about design patterns is that they are generally applicable to all languages. I had thought that lack of typing to an interface (because of the weak typing of PHP) would be a deal-breaker, but I don&#8217;t think so any more. I suppose with a very complex program, you&#8217;d miss stronger typing, but from what I&#8217;ve seen PHP programs with a good structure work well and are easy to change,  are reusable and are loosely coupled. So, in case you&#8217;re considering creating PHP design patterns, I&#8217;d definitely encourage it, and you&#8217;ll find that converting examples from ActionScript 3.0 isn&#8217;t all that difficult.</p>
<p>Naturally, we&#8217;d like to hear from both ActionScript 3.0 and PHP developers to get your thoughts on this topic. We&#8217;d be very interested in applications that use both ActionScript 3.0 and PHP and linking up optimal design patterns so that modules from the two languages can more easily be integrated.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F11%2Fphp-chain-of-responsibility-design-pattern-from-actionscript-30%2F&amp;title=PHP%20Chain%20of%20Responsibility%20Design%20Pattern%20from%20ActionScript%203.0" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/11/php-chain-of-responsibility-design-pattern-from-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 and PHP 5: Doing Design Patterns</title>
		<link>http://www.as3dp.com/2009/05/actionscript-30-and-php-5-doing-design-patterns/</link>
		<comments>http://www.as3dp.com/2009/05/actionscript-30-and-php-5-doing-design-patterns/#comments</comments>
		<pubDate>Mon, 25 May 2009 19:15:40 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=997</guid>
		<description><![CDATA[Note: This is one of those posts that begs for reader comments. PHP is a much-loved language of mine, but I just don’t do the kind of applications that require PHP often. As a result, it stays on the shelf until I need it. So, while very familiar with PHP, I do not claim a [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Note:</strong> This is one of those posts that begs for reader comments. PHP is a much-loved language of mine, but I just don’t do the kind of applications that require PHP often. As a result, it stays on the shelf until I need it. So, while very familiar with PHP, I do not claim a high level expertise in it.  However, judging from the online PHP discussions and PHP publications, lots of developers are well versed in the language. So, for you PHP’ers out there, your comments are welcomed. (Likewise if you’re not familiar with PHP, feel free to comment as well.)</em></p>
<p><strong>PHP, My Old Friend</strong></p>
<p>PHP has always been a developers programming language. From its inception, you could write PHP using Notepad or TextEdit and not have to worry about some cranky API or IDE. Alternatively, there are plenty of development applications for creating PHP, such as Dreamweaver. Further, you could write email apps as easy as a ‘Hello World’ example. Not only is PHP open source, it is constantly updated and improved by developers working on it because it is important to them. You can plunk it down on top of an Apache server (also open source) and use MySQL (also open source—but maybe not for long) for a database. It runs on Linux, Windows and on Mac OS (it comes standard with Macs as does an Apache server).  Further, PHP 5 has real abstract classes! What’s not to like?<br />
<span id="more-997"></span><br />
<strong>Working with Design Patterns in PHP</strong></p>
<p>Not to raise the ire of PHP programmers, but working with design patterns in PHP after getting used to doing so in ActionScript 3.0 (or Java or C#) can be a real challenge. In fact working with OOP principles in a language without typed data can be extremely perplexing. This is not due to the lack of PHP design pattern examples or OOP discussions by top-notch programmers. I found plenty. Rather, when I started working on the PHP portion for the recent <a href= "http://www.as3dp.com/2009/05/22/actionscript-30-design-patterns-at-work-survey/">Design Patterns at Work Survey</a>, I found many unexpected hitches. Naturally, I didn’t want to create a survey on design patterns and <em>not use a design pattern to create it</em>. It was a simple Strategy pattern, but still, it was important to do it with some modicum of integrity. What follows are <em>some</em> of my discoveries and queries about working with PHP where OOP and design patterns are important.</p>
<p><strong>How Can You Program to an Interface Without Typed Variables?</strong></p>
<p>For Gamma, <em> et al</em> the first principle for minimizing dependencies in programming is,</p>
<blockquote><p>Program to an interface, not an implementation</p></blockquote>
<p>In ActionScript 3.0 you have something like the following in programming to an interface:</p>
<p><strong>var instanceNow:MyAbstractClass = new ConcreteClass();</strong></p>
<p>Likewise in Java and C#, you’d see,</p>
<p><strong>MyAbstractClass instanceNow = new ConcreteClass();</strong></p>
<p>All of these languages have strict typing. So how is it possible in PHP— characterized by loose typing—to program to an interface? In looking at numerous examples, all I saw was typing to the <em>implementation</em>. To illustrate, the following example shows building an abstract class with an abstract property and method. To understand this example in PHP, you need to know a couple of things:</p>
<ul>
<li>The <strong>=></strong> operator is the same as a dot (.) in ActionScript 3.0. So<br />&nbsp;&nbsp;&nbsp;<code>$keyInstance->keyMethod()</code><br /> in PHP is the same as<br />    &nbsp;&nbsp;&nbsp;<code>keyInstance.keyMethod()</code><br /> in ActionScript 3.0.</li>
<li>Variables begin with a <strong>$</strong> character. However, when expressed as properties of an object, they do not have the <strong>$</strong> character.</li>
<li>Variables have no typing or <strong>var</strong> statements. They are “declared” in a class using access modifiers (e.g., protected $myVar).</li>
</ul>
<p>If you’re familiar with PHP, the following is pretty simple to follow, and if you’re not, it still should make some sense given the above clues to PHP’s unique features.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p997code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9979"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p997code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> AbClass
<span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$keyProperty</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Abstract methods need elaboration in implementation</span>
&nbsp;
    abstract <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> abFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// All subclasses inherit this method</span>
&nbsp;
 	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> keyMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#123;</span>
        	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">abFunction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">print</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">keyProperty</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ConClass <span style="color: #000000; font-weight: bold;">extends</span> AbClass
<span style="color: #009900;">&#123;</span>
    	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> abFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
        	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;PHP: Detail of abstract method.&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$keyInstance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ConClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$keyInstance</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">keyProperty</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Key Property: Value of property from abstract class.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$keyInstance</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">keyMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>That little bit of code produces the following:</p>
<p><strong><br />
PHP: Detail of abstract method.<br />
Key Property: Value of property from abstract class.<br />
</strong></p>
<p>In ActionScript 3.0, the client would have instantiated the same operations as:<br />
<strong><br />
var keyInstance:AbClass =new ConClass();<br />
</strong></p>
<p>That’s what it means <em>to program to an interface</em>, and the Gang of Four could not have put it any clearer:</p>
<blockquote><p>Don’t declare variables to be instances of particular concrete classes. Instead, commit only to an interface defined by an abstract class. You will find this to be a common theme of the design patterns in this book.</p></blockquote>
<p>So how does one go about creating good PHP that minds its manners as far as OOP principles and design patterns are concerned? In an excerpt from Matt Zandstra’s book, <em>PHP Objects, Patterns, and Practice (Second Edition)</em> I saw the following intriguing kind of code in the context of a Strategy Design Pattern. (Below, I’ve just included a small part and have changed the names to reflect the Strategy pattern structure.)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p997code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p99710"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p997code10"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Context
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$propertyA</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$strategyProp</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$propertyA</span><span style="color: #339933;">,</span> Strategy <span style="color: #000088;">$strategy</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">propertyA</span> <span style="color: #339933;">=</span><span style="color: #000088;">$propertyA</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">strategyProp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$strategy</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> methodA<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">strategyProp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">methodA</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> In the <code>__construct()</code> function, you can see the parameter pair, <code>Strategy $strategy</code>. Now if you’re a Java or C# programmer, you’ll see that as a typed parameter. (It would be <code>strategy:Strategy</code> in ActionScript 3.0.) However, it turns out to be a way of forming an aggregate relationship between the Context and Strategy classes in a Strategy Design Pattern. (Context delegates to Strategy.) That’s exactly what’s supposed to happen in a Strategy design. <em>However</em>, I’m still not sure what <strong>Strategy</strong> actually does in the parameter list. Further, if it acts like a typing statement, could it be used to type instances to interfaces? I don’t know, but it’s interesting, and if any of you OOP PHP folks out there know the answer, let us know.</p>
<p><strong>From a Detour to a Mystery</strong></p>
<p>This post started out as a little discussion about building the ActionScript 3.0 Design Pattern at Work survey and trying to be faithful to all of the principles and patterns we’ve discussed in ActionScript 3.0. It led to some mysteries about PHP and OOP, which I find endlessly fascinating. What better challenge could there be than working out design patterns where it appears to be impossible to follow the most fundamental principle of design pattern development—<em>program to an interface, not an implementation</em>.</p>
<p>One possible answer lies in the O’Reilly book, <em>PHP Design Patterns</em> by Stephan Schmidt. When I went to look for it, it was nowhere to be found! It wasn’t on the O’Reilly list or Amazon.com or anywhere. Since I’m one to panic at the drop of a hat, I assumed it has been abducted by aliens, but no. It’s written in German and can be found on the German version of Amazon .com.  That’s the end of a promising mystery. Let’s hope it’s translated into English, Spanish, Portuguese and French for us New World types!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F05%2Factionscript-30-and-php-5-doing-design-patterns%2F&amp;title=ActionScript%203.0%20and%20PHP%205%3A%20Doing%20Design%20Patterns" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/05/actionscript-30-and-php-5-doing-design-patterns/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

