I don't understand why, in the code below, $my_foo and $my_bar are correctly inherited by the child class, but if I change $my_foo by assigning a reference to $my_var, the child class still sees the original value..
<?php
class Foo
{
public static $my_foo = 'foo';
public static $my_bar = 'bar';
public static function break_inheritance() {
self::$my_bar = &self::$my_foo;
}
public static function foo_print_vars() {
print self::$my_foo." ";
print self::$my_bar."\n";
}
}
class Bar extends Foo
{
public static function bar_print_vars() {
print self::$my_foo." ";
print self::$my_bar."\n";
}
}
Bar::bar_print_vars(); // OUTPUTS foo bar
Foo::break_inheritance();
Foo::foo_print_vars(); // OUTPUTS foo foo
Bar::bar_print_vars(); // OUTPUTS foo bar
EDIT: this is a similar question: do extended classes inherit static var values (PHP)? but mine is more focused on inheritance and references.
EDIT2: please note that the point of this question is not about late static binding, but it's why, since $my_foo and $my_bar are inherited, changing them in Foo doesn't affect them when accessed in Bar. And this only happens with references. In fact if we change:
public static function break_inheritance() {
self::$my_bar = self::$my_foo; // removed reference in assignment
}
the behavior totally changes and the last Bar::bar_print_vars(); // OUTPUTS foo foo