<?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>Mike Conley&#039;s Blog &#187; model</title>
	<atom:link href="http://mikeconley.ca/blog/tag/model/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikeconley.ca/blog</link>
	<description>The personal blog of a Toronto based software developer, musician, sound designer, and theatre enthusiast.</description>
	<lastBuildDate>Tue, 10 Jan 2012 13:58:35 +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>Model-View-Controller in PHP:  Model</title>
		<link>http://mikeconley.ca/blog/2009/02/13/model-view-controller-in-php-model/</link>
		<comments>http://mikeconley.ca/blog/2009/02/13/model-view-controller-in-php-model/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 16:34:19 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[late static binding]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[model-view-controller]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[rowdatagateway]]></category>

		<guid isPermaLink="false">http://mikeconley.ca/blog/?p=93</guid>
		<description><![CDATA[(Notes: the code here has been taken directly from the PHP Late Static Binding documentation if you want a more verbose and thorough explanation of this problem, check this out ) I have one thing to say for all of those PHP developers who look at Rails&#8217; ActiveRecord class and get all excited about implementing [...]]]></description>
			<content:encoded><![CDATA[<p>(Notes:</p>
<ul>
<li>the code here has been taken directly from the <a href="http://ca2.php.net/oop5.late-static-bindings" target="_self">PHP Late Static Binding documentation</a></li>
<li>if you want a more verbose and thorough explanation of this problem, <a href="http://www.actsasflinn.com/trackbacks?article_id=php-and-activerecord&amp;day=08&amp;month=08&amp;year=2007 " target="_self">check this out</a></li>
</ul>
<p>)</p>
<p>I have one thing to say for all of those PHP developers who look at Rails&#8217; ActiveRecord class and get all excited about implementing it in PHP:</p>
<p>It can&#8217;t be done.  Yet.</p>
<p>Here&#8217;s why:</p>
<p>Until PHP5.3, PHP does not implement a feature called &#8220;late static binding&#8221;.  What is late static binding?  Well, how about I show you what it&#8217;s like to NOT have late static binding:</p>
<pre>class A {
   public static function who() {
     echo __CLASS__;
   }
   public static function test() {
     self::who();
  }
}
class B extends A {
  public static function who() {
    echo __CLASS__;
  }
}
B::test();  //Outputs:  'A'</pre>
<p>That&#8217;s right:  B::test() outputs &#8216;A&#8217;.  This is a problem, because while it is true that B is a subclass of A, B is still B.  When I call a static method of B, I want it to know that it&#8217;s B.</p>
<p>With late static binding (only available in PHp5.3 and onward), this goes away:</p>
<pre>&lt;?php
class A {
  public static function who() {
    echo __CLASS__;
  }
  public static function test() {
    static::who(); // Here comes Late Static Bindings
  }
}
class B extends A {
  public static function who() {
    echo __CLASS__;
  }
}
B::test();  //Outputs 'B'
?&gt;</pre>
<p>Why is this a problem for ActiveRecord?  Well, say we define a class called ActiveRecord, and create a subclass of ActiveRecord called Person.  When I call Person::find_all(), PHP5.2 is going to run find_all in ActiveRecord &#8211; and ActiveRecord&#8217;s find_all() will not know what kind of subclass I am.  It won&#8217;t know if I&#8217;m a Person, Dog, Pizza, or any of that jazz.  Essentially, ActiveRecord is now dead in the water.</p>
<p>Now, you could just make find_all a standard method instead of a static one, but then for every find operation, you&#8217;d have to do this:</p>
<pre>$p = new Person();
$persons = $p-&gt;find_all();</pre>
<p>Semantically, this doesn&#8217;t make much sense.</p>
<p><em>But, PHP coders, take heart -  there are two silver linings:</em></p>
<ul>
<li>PHP5.3 has late static bindings!  As soon as it&#8217;s released, rejoice, and implement ActiveRecord</li>
<li><a href="http://www.actsasflinn.com/2007/08/08/php-and-activerecord" target="_self">There&#8217;s always RowDataGateway!</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://mikeconley.ca/blog/2009/02/13/model-view-controller-in-php-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

