<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>Crisis Averted!: PHP 5, static and inheritance</title>
    <link>http://socket7.net</link>
    <description>Crisis Averted!  A weblog by Brett Stimmerman.</description>
    <managingEditor>brettstimmerman@gmail.com (Brett Stimmerman)</managingEditor>
    <webMaster>brettstimmerman@gmail.com (Brett Stimmerman)</webMaster>
    <docs>http://backend.userland.com/rss</docs>
    <ttl>60</ttl>

  <item>
  <title>Re: Parent</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-230</link>
  <author>Brett (http://socket7.net)</author>
  <description>
    &lt;p&gt;Can you explain your solution a bit more? Swapping declarations as I assume you are suggesting is syntactically impossible. And even if it was possible it overwrites the parent's initial value, which is unacceptable.&lt;/p&gt;
&lt;pre&gt;
class Cat extends Animal {
  parent::$color = 'Brown';
}
&lt;/pre&gt;  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-230</guid>
  <pubDate>Tue, 18 Aug 2009 20:19:12 -0700</pubDate>
</item>
  <item>
  <title>Parent</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-229</link>
  <author>jagged software (http://facebook.com/jaggedsoft)</author>
  <description>
    When using this example, your animals should say parent::$color = 'Brown';  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-229</guid>
  <pubDate>Tue, 18 Aug 2009 14:31:43 -0700</pubDate>
</item>
  <item>
  <title>More info on this issue</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-228</link>
  <author>Robert</author>
  <description>
    http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-228</guid>
  <pubDate>Tue, 11 Aug 2009 00:12:33 -0700</pubDate>
</item>
  <item>
  <title>No title</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-227</link>
  <author>Snap</author>
  <description>
    Thank you all for you workarounds :)  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-227</guid>
  <pubDate>Tue, 23 Jun 2009 00:18:03 -0700</pubDate>
</item>
  <item>
  <title>Re: My Solution</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-220</link>
  <author>bluedes (http://bluediamondsfx.181l.com)</author>
  <description>
    That's nice, but when you have too many members, your array will get big.  Accessing associative arrays is costly.&lt;br /&gt;
&lt;br /&gt;
But still nice. ^_^  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-220</guid>
  <pubDate>Wed, 18 Mar 2009 07:11:30 -0700</pubDate>
</item>
  <item>
  <title>the singleton pattern</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-214</link>
  <author>chris (http://www.ncoded.com)</author>
  <description>
    ok &quot;i think&quot; that the way to do static members etc, is to use the singleton pattern; its definitly one of these patterns.&lt;br /&gt;
&lt;br /&gt;
ok just to be clear, by static, i mean a data member that remembers its state across classes. 
&lt;br /&gt;&lt;br /&gt;
to be honest i cant remember if it holds up on inhertitance, i think you may have to use interfaces for that, not sure tho.
&lt;br /&gt;&lt;br /&gt;
i am working tho a project at the momment which needs static members, so will update this post (if i remember) when i solve this.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-214</guid>
  <pubDate>Thu, 12 Feb 2009 09:45:14 -0800</pubDate>
</item>
  <item>
  <title>No title</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-143</link>
  <author>blaze</author>
  <description>
    Ruby, in this aspect, is doing the right thing.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-143</guid>
  <pubDate>Fri, 26 Sep 2008 00:56:59 -0700</pubDate>
</item>
  <item>
  <title>Catching Undefined Method errors</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-138</link>
  <author>@KEM</author>
  <description>
    Undefined methods will raise an E_FATAL_ERROR - the kind you can't catch.&lt;br /&gt;
&lt;br /&gt;
However, as PHP is magical, there are various &quot;Magic&quot; function in PHP.&lt;br /&gt;
&lt;br /&gt;
Check out the __callStatic magic function. This trick, in combination with class inheritance may provide you the functionality you are looking for.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-138</guid>
  <pubDate>Tue, 23 Sep 2008 08:49:54 -0700</pubDate>
</item>
  <item>
  <title>Re: Web Developer</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-137</link>
  <author>Brett (http://socket7.net)</author>
  <description>
    Well, it's now August 2008, and PHP 5.3 is only in alpha.  At least it's made the cut, along with a handful of other useful (and long-needed) features.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-137</guid>
  <pubDate>Wed, 27 Aug 2008 14:10:24 -0700</pubDate>
</item>
  <item>
  <title>Web Developer</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-68</link>
  <author>Matthew Grdinic (http://www.sledxchange.com)</author>
  <description>
    PHP 5.3&lt;br /&gt;
&lt;a href=&quot;http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html&quot;&gt;Late Static Binding&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Supposed to be out early 2008.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-68</guid>
  <pubDate>Tue, 18 Dec 2007 16:55:12 -0800</pubDate>
</item>
  <item>
  <title>Re: No title</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-67</link>
  <author>Brett (http://socket7.net)</author>
  <description>
    Thanks for pointing this out Ben.  There is hope!  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-67</guid>
  <pubDate>Mon, 03 Dec 2007 13:12:18 -0800</pubDate>
</item>
  <item>
  <title>No title</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-66</link>
  <author>Ben (http://www.benlog.org)</author>
  <description>
    &lt;a href=&quot;http://bugs.php.net/bug.php?id=42681&quot;&gt;PHP bug #42681&lt;/a&gt; - status is 'assigned'.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-66</guid>
  <pubDate>Sat, 01 Dec 2007 19:43:59 -0800</pubDate>
</item>
  <item>
  <title>If the class is instantiated</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-65</link>
  <author>James</author>
  <description>
    blah&lt;br /&gt;
&lt;br /&gt;
propertyDefinitions = self::$propertyDefinitions;&lt;br /&gt;
&lt;br /&gt;
// propertyDefinitions);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
However I doubt this is the best solution. There should be the ability to do&lt;br /&gt;
$className::whatever without eval or something :/  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-65</guid>
  <pubDate>Wed, 28 Nov 2007 00:43:48 -0800</pubDate>
</item>
  <item>
  <title>Alternative to Tom's method</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-55</link>
  <author>toby (http://bezoe.com)</author>
  <description>
    I've used this method for getting a calling subclass's class constants.  The problem is that the interpretor will not catch syntax errors in the eval until the statement is executed.&lt;br /&gt;
&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
$className = get_class($this);&lt;br /&gt;
return eval(&quot;return $className::table;&quot;);&lt;br /&gt;
&lt;/code&gt;  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-55</guid>
  <pubDate>Mon, 22 Oct 2007 01:03:37 -0700</pubDate>
</item>
  <item>
  <title>Possibility</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-45</link>
  <author>KEM</author>
  <description>
    &lt;p&gt;this has been irking me for a while.&lt;/p&gt;
&lt;p&gt;anybody know if you can catch &quot;method undefined&quot; errors with set_error_handler? no luck yet... i'm not exactly sure what kind of error that is. it may not be &quot;catchable&quot;.&lt;/p&gt;
&lt;p&gt;dirty, but (hypothetically):&lt;/p&gt;
&lt;pre&gt;
&amp;lt;?php
	function myErrorHandler($errno, $errstr){
		$info = debug_backtrace();
		// if called on a subclass of Record, divert to a call to 
		// Table::find with the correct $table argument
	}
	set_error_handler(&amp;apos;myErrorHandler&amp;apos;);

	class Table {
		public static function find($table){
			// ...
		}
	}
	
	// dummy
	abstract class Record {}
	
	class Person extends Record {
		public static $table = &amp;apos;People&amp;apos;;
	}
	
	Person::find();

?&amp;gt;

&lt;/pre&gt;

&lt;p&gt;that would keep the classes nice and clean&amp;hellip;&lt;/p&gt;  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-45</guid>
  <pubDate>Sat, 18 Aug 2007 20:34:31 -0700</pubDate>
</item>
  <item>
  <title>Re: My solution</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-38</link>
  <author>Brett (http://socket7.net)</author>
  <description>
    I like your solution for its brevity.  I also like that for each new class it's only a small method to implement; it's very well contained. I've never liked my solution of passing variables around like a fool; that's incredibly messy.&lt;br /&gt;
&lt;br /&gt;
Thanks for sharing.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-38</guid>
  <pubDate>Sun, 15 Jul 2007 19:41:08 -0700</pubDate>
</item>
  <item>
  <title>My Solution</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-37</link>
  <author>Tom Lancaster (http://www.newhanoian.com)</author>
  <description>
    &lt;pre&gt;
static $table;

 protected function table() {
	$vars = get_class_vars(get_class($this));
	return $vars['table'];
}
&lt;/pre&gt;  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-37</guid>
  <pubDate>Wed, 11 Jul 2007 22:25:42 -0700</pubDate>
</item>
  <item>
  <title>Exactly the same problem</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-33</link>
  <author>Jonovic</author>
  <description>
    This is realy pain in the a.. My solution for this time is to turn to dynamic objects and forget the static classes for a while. I have been working around the database access objects with the help of design patterns.  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-33</guid>
  <pubDate>Mon, 09 Jul 2007 11:43:02 -0700</pubDate>
</item>
  <item>
  <title>Re: Workaround?</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-19</link>
  <author>Brett (http://socket7.net)</author>
  <description>
    &lt;p&gt;I worked around it in two ways, neither of which are great.  One of the ways is to set a member variable in the constructor.  This makes the value available within the object as well as from the outside, though it is not static.  Obviously not a great solution.&lt;/p&gt;

&lt;p&gt;The second way I've worked around this is to pass an extra class name parameter into the static method calls telling the method which class it should 'act as.'  Again, not a great solution.&lt;/p&gt;

&lt;p&gt;If you're willing to put up with the lack of purity, these two options work well enough.  Hopefully some future version of PHP will address this.&lt;/p&gt;  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-19</guid>
  <pubDate>Sat, 05 May 2007 14:54:34 -0700</pubDate>
</item>
  <item>
  <title>Workaround?</title>
  <link>http://socket7.net/article/php-5-static-and-inheritance#comment-18</link>
  <author>Tom Lancaster (http://www.newhanoian.com)</author>
  <description>
    &lt;p&gt;I'm having this exact same problem. In my case I'm subclassing  an abstract acts&lt;em&gt;as&lt;/em&gt;tree for different db tables. Of course I have the table name as a static member, and of course it doesn't work.&lt;/p&gt;

&lt;p&gt;I'm interested to know how you work around this.&lt;/p&gt;
  </description>
  <guid isPermaLink="true">http://socket7.net/article/php-5-static-and-inheritance#comment-18</guid>
  <pubDate>Wed, 02 May 2007 22:20:27 -0700</pubDate>
</item>

  </channel>
</rss>
<!-- 0.0354 -->