<?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>Perseverance Trumps Talent &#187; object-oriented programming</title>
	<atom:link href="http://aaroncollegeman.com/tag/object-oriented-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://aaroncollegeman.com</link>
	<description></description>
	<lastBuildDate>Thu, 11 Feb 2010 22:26:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Static class awareness coming to PHP 5</title>
		<link>http://aaroncollegeman.com/2009/02/static-class-awareness-coming-to-php-5/</link>
		<comments>http://aaroncollegeman.com/2009/02/static-class-awareness-coming-to-php-5/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 06:07:02 +0000</pubDate>
		<dc:creator>Aaron</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[object-oriented programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scottlib]]></category>

		<guid isPermaLink="false">http://aaroncollegeman.com/?p=265</guid>
		<description><![CDATA[From time to time, I go digging through the PHP documentation on PHP.net hoping to discover that the PHP commiters have added better (read: more standardized) support for static properties.  PHP&#8217;s implementation of object-oriented programming is good, if a little incomplete and inconsistent with other good object-oriented languages (like Java, for instance &#8211; no pun [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time, I go digging through the PHP documentation on <a href="http://us3.php.net/docs.php">PHP.net</a> hoping to discover that the PHP commiters have added better (read: more standardized) support for static properties.  PHP&#8217;s implementation of object-oriented programming is good, if a little incomplete and inconsistent with other good object-oriented languages (like Java, for instance &#8211; no pun intended).</p>
<p>PHP&#8217;s handling of static properties in one such shortcoming.  Typically, properties and methods defined statically are bound to the class definition, and are accessed using a class&#8217; name instead of an instance of it.  For example, a static property named <em>foo</em> of class <em>Bar</em> is accessed <em>Bar::$foo</em>.  Similarly, a static method named <em>world</em> of class <em>Hello</em> is accessed  <em>Hello::world()</em>;  PHP incorporates these ideas in a natural fashion.</p>
<p>It is in subclassing that PHP falls all over itself.</p>
<p>Let&#8217;s say I want to create a static property named <em>foo</em> on a class <em>Bar. </em>The class definition would look something like this</p>
<div class="geshi"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'value'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre><textarea onfocus="jQuery(this).select();" onblur="jQuery(this).hide().prev().show();" style="display:none;">class Bar {
    static $foo = 'value';
}</textarea><a class="clipboard" href="javascript:;" onclick="jQuery(window).scrollTo(jQuery(this).prev().show().focus().prev().hide().parent(), 500);">copy code</a></div>
<p>Now, let&#8217;s say I want to create a subclass of Bar called <em>BarSubclass</em>.  Typically, if I wanted to provide a new value for the static property <em>foo</em>, I would simply override it in my subclass&#8217; definition like this</p>
<div class="geshi"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> BarSubclass <span style="color: #000000; font-weight: bold;">extends</span> Bar <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'new_value'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre><textarea onfocus="jQuery(this).select();" onblur="jQuery(this).hide().prev().show();" style="display:none;">class BarSubclass extends Bar {
    static $foo = 'new_value';
}</textarea><a class="clipboard" href="javascript:;" onclick="jQuery(window).scrollTo(jQuery(this).prev().show().focus().prev().hide().parent(), 500);">copy code</a></div>
<p>One would then expect the following results</p>
<div class="geshi"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> Bar<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &quot;value&quot;</span>
<span style="color: #b1b100;">echo</span> BarSubclass<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &quot;new_value&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span>Bar<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span> <span style="color: #339933;">!=</span> BarSubclass<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &quot;1&quot; (for true)</span></pre><textarea onfocus="jQuery(this).select();" onblur="jQuery(this).hide().prev().show();" style="display:none;">echo Bar::$foo; // "value"
echo BarSubclass::$foo; // "new_value"
echo (Bar::$foo != BarSubclass::$foo); // "1" (for true)</textarea><a class="clipboard" href="javascript:;" onclick="jQuery(window).scrollTo(jQuery(this).prev().show().focus().prev().hide().parent(), 500);">copy code</a></div>
<p>Unfortunately, in PHP land, one gets the following output instead</p>
<div class="geshi"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> Bar<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &quot;value&quot;</span>
<span style="color: #b1b100;">echo</span> BarSubclass<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &quot;value&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span>Bar<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span> <span style="color: #339933;">!=</span> BarSubclass<span style="color: #339933;">::</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &quot;&quot; (nothing - for false)</span></pre><textarea onfocus="jQuery(this).select();" onblur="jQuery(this).hide().prev().show();" style="display:none;">echo Bar::$foo; // "value"
echo BarSubclass::$foo; // "value"
echo (Bar::$foo != BarSubclass::$foo); // "" (nothing - for false)</textarea><a class="clipboard" href="javascript:;" onclick="jQuery(window).scrollTo(jQuery(this).prev().show().focus().prev().hide().parent(), 500);">copy code</a></div>
<p>The reasoning behind this doesn&#8217;t really matter to me.  I&#8217;m sure it has something to do with PHP&#8217;s inventors interpreting the word &#8220;static&#8221; to mean not only &#8220;belonging to the class,&#8221; but also &#8220;not overridable.&#8221;</p>
<p>This evening I received a glimmer of hope.  On February 13 someone updated the PHP docs, and in those docs is a new method: <em>get_called_class()</em>.  This method, callable only from within the bodies of class methods, returns the name of the class in which the function is being invoked.  The function can discern the name of the class in which it is being called, even if the method is statically defined.  This means that at long last, I can define static methods that correctly identify the class in which they are being called.</p>
<p>Why is this important?</p>
<p>A great feature for an ORM library (like scottlib) would be a static <em>get</em> method through which callers could load indexed instances of objects by their primary key values, e.g.</p>
<div class="geshi"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> MyObject<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span>42<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre><textarea onfocus="jQuery(this).select();" onblur="jQuery(this).hide().prev().show();" style="display:none;">$obj = MyObject::get(42);</textarea><a class="clipboard" href="javascript:;" onclick="jQuery(window).scrollTo(jQuery(this).prev().show().focus().prev().hide().parent(), 500);">copy code</a></div>
<p>Until <em>get_called_class(), </em>this was impossible because the definition of the <em>get</em> method existed only at the highest level of the class hierarchy, and there, would be unaware of any subclass that might want to produce an instance of itself instead of the supreme parent.</p>
<p>The only drawback here is that we probably won&#8217;t be seeing this functionality in our hosting any time soon.  The feature is planned for version 5.3.0.  PHP is presently in version 5.2.8 (my Mac came with version 5.2.6).  In the meantime I&#8217;ll be adding static loaders and method calls to scottlib, each with an extra parameter for the class name.  Once PHP 5.3.0 hits production, the extra parameter will be phased out in favor of <em>get_called_class().</em></p>
<p>Hold onto your keyboards &#8211; PHP is about to get a whole lot cooler.</p>
]]></content:encoded>
			<wfw:commentRss>http://aaroncollegeman.com/2009/02/static-class-awareness-coming-to-php-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
