18 Apr
I’ve read a few opinionated pieces that mention how learning to program in PHP can make it difficult to pick up advanced object-oriented techniques down the road.
Is the reverse also true? Behaviour I’ve come to expect from other high level languages doesn’t always seem to translate to PHP.
Consider this simple (but meaningless) program written in Ruby:
<pre>class MyParent
@classname = 'Parent' # static member variable
end
class MyChild < MyParent
@classname = ‘Child’ # override the static member variable
end
puts MyParent::classname # gives me ‘Parent’
puts MyChild::classname # gives me ‘Child’
Makes sense, right? Consider the same program, this time written in PHP:
class MyParent {
static public $classname = 'Parent';
static function classname() {
return self::$classname;
}
}
class MyChild extends MyParent {
static public $classname = 'Child';
}
echo MyParent::classname(); # gives me 'Parent'
echo MyChild::classname(); # gives me 'Parent'
Which behaviour is “right”? Or is that even a fair evaluation?
As a side note, some folks in #php on freenode came up with this workaround, which seems to defeat the whole purpose of subclassing anyways.