Why does assigning a reference to a static variable of a class interrupt inheritance of such a variable?

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: so that extended classes inherit static var values ​​(PHP)? but mine are more focused on inheritance and links.

EDIT2: note that the point of this question is not late static binding, but why, because $ my_foo and $ my_bar are inherited, changing them in Foo does not affect them when accessing the Bar. And this only happens with links . In fact, if we change:

 public static function break_inheritance() {
            self::$my_bar = self::$my_foo; // removed reference in assignment
        }

, Bar:: bar_print_vars();//OUTPUTS foo foo

+4
3

Foo - , Bar. Bar::break_inheritance(); , .

0

.

class Foo
{
    public static $my_foo = 'foo';
    public static $my_bar = 'bar';
    // ...
}

Foo, Foo::$my_foo Foo::$my_bar.

Foo, Bar Bar::$my_foo Bar::$my_bar.

:

class Foo
{
    public static function break_inheritance() {
        self::$my_bar = &self::$my_foo;
    }
}
Foo::break_inheritance(); 

Foo::$my_bar. Bar::$my_bar.

:

Bar::break_inheritance(); 

- Foo::$my_bar Bar::$my_bar.

, self::, , . , Foo::$my_bar = &Foo::$my_foo;. , break_inheritance(), Foo::$my_bar.

break_inheritance() Bar::$my_bar, , static:: self:::

class Foo
{
    public static function break_inheritance() {
        static::$my_bar = &static::$my_foo;
    }
}
Foo::break_inheritance();         // Changes `Foo::$my_bar`
Bar::break_inheritance();         // Changes `Bar::$my_bar`

late static binding PHP 5.3.

(static::) , $this-> . , , ( ), , , .

0

, PHP 5.3.0 .

Late static binding - , static:: , , .

0

All Articles