<?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>Professional PHP &#187; closures</title>
	<atom:link href="http://www.procata.com/blog/archives/tag/closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.procata.com/blog</link>
	<description>PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.</description>
	<lastBuildDate>Fri, 10 Dec 2010 17:23:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Closures are coming to PHP</title>
		<link>http://www.procata.com/blog/archives/2009/03/22/closures-are-coming-to-php/</link>
		<comments>http://www.procata.com/blog/archives/2009/03/22/closures-are-coming-to-php/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 22:09:16 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/?p=293</guid>
		<description><![CDATA[Dagfinn has a post looking at using the new closure feature of PHP 5.3.  He compares using foreach for iteration versus array_map. &#8220;Interesting,&#8221; he concludes, &#8220;but not necessarily better than conventional alternatives.&#8221;
I agree for that case.  Consider instead, a more complicated operation that requires a setup and a tear down after.
&#160;
setup&#40;&#41;;
operation&#40;&#41;;
teardown&#40;&#41;;
&#160;
Now what happens [...]]]></description>
			<content:encoded><![CDATA[<p>Dagfinn has a <a href="http://blog.agilephp.com/2009/03/19/real-programming-with-php-53-part-1-array-processing/">post</a> looking at using the new closure feature of PHP 5.3.  He compares using foreach for iteration versus array_map. &#8220;Interesting,&#8221; he concludes, &#8220;but not necessarily better than conventional alternatives.&#8221;</p>
<p>I agree for that case.  Consider instead, a more complicated operation that requires a setup and a tear down after.<br />
<pre class="php">&nbsp;
setup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
operation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
teardown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre></p>
<p>Now what happens if we need to be able to customize operation?  That&#8217;s common enough, one way of doing this is to create a template method.</p>
<p><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyExample <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> operation<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: #000000; font-weight: bold;">function</span> setup<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: #000000; font-weight: bold;">function</span> teardown<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: #000000; font-weight: bold;">function</span> doit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setup</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">operation</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">teardown</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;</pre></p>
<p>Now, we can subclass MyExample and override operation() with custom logic.  This is well and good, but what the customization we need is fairly small.  Creating a new class carries a certain weight.  Especially if you are religious about one class per file.  </p>
<p><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyExampleExtension <span style="color: #000000; font-weight: bold;">extends</span> MyExample <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> operation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// custom logic</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre></p>
<p>Plus, you now have to deal with some creational patterns to make sure your custom class is used in the right context.</p>
<p><pre class="php">&nbsp;
<span style="color: #0000ff;">$myObject</span> = <span style="color: #0000ff;">$registery</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'MyExample'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$myObject</span>-&gt;<span style="color: #006600;">doit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre></p>
<p>So, instead of encapsulating the pattern, its also very common to just copy and paste:</p>
<p><pre class="php">&nbsp;
setup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
custom_operation1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
teardown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//...</span>
setup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
custom_operation2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
teardown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre></p>
<p>But that&#8217;s not good on the duplicate code front.  So here is an alternate implementation, but using a <del datetime="2009-03-24T17:34:07+00:00">closure</del> anonymous function as a callback.</p>
<p><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyExample2 <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> setup<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: #000000; font-weight: bold;">function</span> teardown<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: #000000; font-weight: bold;">function</span> doit<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$operation</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setup</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$operation</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">teardown</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;</pre></p>
<p>The advantage of MyExample2 is that extending is that the setup and teardown pattern is encapsulated in one spot.  You konw that if setup is called, teardown will also be called.  But, extending the operation is very light weight.</p>
<p><pre class="php">&nbsp;
<span style="color: #0000ff;">$myObject</span> = <span style="color: #000000; font-weight: bold;">new</span> MyExample2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$myObject</span>-&gt;<span style="color: #006600;">doIt</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">/* custom logic 1 */</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// ...</span>
<span style="color: #0000ff;">$myObject</span>-&gt;<span style="color: #006600;">doIt</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">/* custom logic 2 */</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre></p>
<p>There is  another significant benefit to this and that is locality of reference.  Here, the custom1 logic and the custom2 logic appears in context, not far away in some custom class or function declaration.  So you get encapsulation and reuse for the common code parts, but without the sprawl and overhead of declaring structures that will only be used once in a context far away their declaration.</p>
<p>Closures and anonymous functions decrease the activation energy to write good code.</p>
<p>That&#8217;s not to say that closures and anonymous functions can&#8217;t be abused.  If you keep seeing the same logic over and over in an anonymous block, you should probably give it a name in the form of a class, method or function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2009/03/22/closures-are-coming-to-php/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
		<item>
		<title>OOP is Mature, not Dead</title>
		<link>http://www.procata.com/blog/archives/2007/01/07/oop-is-mature-not-dead/</link>
		<comments>http://www.procata.com/blog/archives/2007/01/07/oop-is-mature-not-dead/#comments</comments>
		<pubDate>Sun, 07 Jan 2007 20:51:16 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[late-static-binding]]></category>
		<category><![CDATA[object-oriented-programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[uniform-access-principle]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/archives/2007/01/07/oop-is-mature-not-dead/</guid>
		<description><![CDATA[I ran across an interesting series of blog posts by Karsten Wagner claiming that OOP is dead (part 2 and part 3).  The premise behind these posts is that OOP has failed to deliver and that it is on the decline in favor of more functional or meta programming techniques.  Maybe its true [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across an interesting series of blog posts by Karsten Wagner claiming that <a href="http://kawagner.blogspot.com/2006/08/oop-is-dead.html">OOP is dead</a> (<a href="http://kawagner.blogspot.com/2006/08/oop-is-dead-part-2.html">part 2</a> and <a href="http://kawagner.blogspot.com/2006/08/oop-is-dead-part-3.html">part 3</a>).  The premise behind these posts is that OOP has failed to deliver and that it is on the decline in favor of more functional or meta programming techniques.  Maybe its true that the discussion of the merits of OOP is on the decline.  At least if you read <a href="http://reddit.com/">reddit</a>.</p>
<p>However, OOP is not on the decline.  Quite simply, it has become mature.  The discussion may be on the decline because almost every language that anyone actually uses implements a core set of OOP features.  OOP has won its arguments.  Good luck taking a language mainstream without it.</p>
<p>Oh, yeah, there are some OOP features that are still controversial or unusual.  There is the single versus multiple inheritance debate, or perhaps Ruby&#8217;s open classes.  But, I think these things have a way of cross-pollinating across the popular languages when they make sense.</p>
<p>A good example of this cross-pollination is happening now with properties, accessor methods and the <a href="http://en.wikipedia.org/wiki/Uniform_access_principle">uniform access principle</a>.  Language support for declared accessor method is slowly creeping across all of the major languages.  Not that Objective C is all that popular, but <a href="http://developer.apple.com/leopard/overview/tools.html">Objective C 2.0</a> adds support for &#8216;em.  Even stodgy old Java is considering <a href="http://www.javalobby.org/java/forums/t88090.html">language level property support</a>.</p>
<p>Sadly, PHP does not yet have language support for declared properties with accessor methods.  What are <a href="http://us2.php.net/manual/en/language.oop5.overloading.php">__get and __set</a>?  They&#8217;re property missing handlers, not accessor methods.  You can simulate accessor methods with them, but that is a poor solution for most applications.  There is no way to support differing visibility, for example protected setters and public getters.  Property not found handlers are prohibitively verbose to write, have a poor performance profile, have no capability for reflection, cause interoperability problems, and have inheritance edge case gotchas (not present in the java beans model, for example).  My hope is to see good language support for properties in PHP 6.</p>
<p>Closures may not be object oriented, but they seem to be undergoing that same language cross-pollination.  Thats seems to be a pretty good sign that they are useful.  It doesn&#8217;t have to be closures OR objects, it can be closures AND objects.  We can use each when they make sense.</p>
<p>Closures are another wish list item for PHP 6.  PHP is almost wired for them with its <a href="http://us2.php.net/callback">callback</a> psuedo type.  Everywhere you can use a callback in PHP, you could use a closure.  I&#8217;d like to see the callback Pinocchio become a real boy like integer or boolean.  The cool thing is that with PHP&#8217;s weak typing the string and array forms of the callback pseudo-type can automatically be converted to a native closure type when needed.</p>
<p>As I said, the core OOP features that most programmers use are in all the mainstream languages.  The interesting part is how they handle the OOP edge cases.  This is the space where the framework developers live.  As I wrote in <a href="http://www.procata.com/blog/archives/2006/01/13/building-a-culture-of-objects-in-php/">culture of objects</a>, PHP has some problems here.  In some ways I think Ruby&#8217;s support for edge cases is exactly what allow a framework such as rails to be built in it, although, I&#8217;m not familiar enough with Ruby to say for certain.</p>
<p>I think addressing some of these issues in PHP 6 will make it a Ruby killer for web applications.  It isn&#8217;t necessary to be perfect here, just to be good enough and allow the larger community, distribution, and stability to take over.  Unfortunately, there is a long lead time here.  If PHP 6  were to add support for declared accessor methods, closures, and late static binding &#8212; my top three framework enablers &#8212; it would still be at least 2-3 years before PHP 6 was sufficiently deployed and the frameworks could adapt to the new features.</p>
<p>In the meantime, while the PHP culture may have problems, the Ruby culture may not be without its own problems.  The influx of <a href="http://www.randomhacks.net/articles/2005/12/03/why-ruby-is-an-acceptable-lisp">lisp</a> and smalltalk programmers, two languages that did not go &#8220;mainstream&#8221; may <a href="http://beust.com/weblog/archives/000382.html">prevent Ruby from going mainstream</a>.  Take a look at <a href="http://blog.lostlake.org/index.php?/archives/11-The-Impending-Ruby-Fracture.html">The impending ruby fracture</a>.  Isn&#8217;t this one of the things that happened to SmallTalk and Lisp?  I&#8217;m still not convinced that Ruby hasn&#8217;t inherited many of the same maintenance problems from its Perl heritage.  Just like english, huh?  Only time will reveal Ruby&#8217;s maintenance characteristics.  I give it about 2 to 3 years for today&#8217;s Rails systems to hit full legacy mode. How long do you think it will take for top notch unicode support in Ruby?</p>
<p>Obviously PHP 6 is all about teh unicode.  Including an opcode cache is going to be an important performance and adoption driver.  However, I&#8217;d like to see more progress on framework enablers.  I really want to see these in the next major PHP deployment cycle and not in the PHP 7 deployment cycle.  Are there framework enablers other than closures, declared property accessors and late static binding that I have overlooked?</p>
<p>I have high hopes for PHP 6 as a mature and mainstream language.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2007/01/07/oop-is-mature-not-dead/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

