Home Archives Pseudo FAQ
2009.09.01

Inheritance of Static Properties

Seriously, PHP, it's not that hard...

Python (right):
class A:
      spam = 'eggs'
class B(A):
      pass
B.spam = 'meat'
print A.spam     # prints "eggs"

PHP (wrong):
class A {
      public static $spam = 'eggs';
}
class B extends A {
}
B::$spam = 'meat';
echo A::$spam;     // prints "meat"

Sigh.

comments(5) | permalink

Posted by at 1:02 p.m.

The issue here is broader than inheritance, but down to the different variable models of Python and PHP.

In Python, everything is an object that can have any number of variables that refer to it, and assignment can never change anything about the variable, which is merely a label that can be moved around at your whim.

I am less familiar with PHP's semantics, but I understand that it has a more traditional variable model, such that $foo is really something and that you can have its value or references to it. (In Python, there are only references everywhere, really.) For PHP to support what you want, it would have to copy all the stuff in A when you make it, which might be a whole lot of stuff and not what you want much of the time as well.

There are many parts of PHP I would say are awful ideas, but these semantics for variables seem as legitimate to me as Python's way of doing it. Working within that system, this response makes more sense to me than copying everything, though this effect is evil (and screams never-change-anything-you-inherited, I suppose). Perhaps I'm missing some aspect of PHP that makes your desired behavior possible as something consistent with the rest of PHP.

Also, inherit your classes from object.

Posted by on 2009.09.13 at 11:11 p.m.

Stop lying about preterism being true you ignorant moron, stop treating God's word casually you ignorant moron, stop treating history casually you ignorant moron, stop parroting lies carelessly you ignorant moron, stop perpetuating the 70 AD myth you idiot. Read carefully for once you stupid idiot instead of casually farting your opinions all over the net and spreading error concerning God:

http://jesus-messiah.com/html/preterism-preachers.html http://www.bethelministries.com/preterism.htm http://www.therefinersfire.org/preterism.htm

Posted by on 2009.10.27 at 12:52 a.m.

Post a Comment