php - why does assigning a reference to a static class variable break inheritance of such variable? -


i don't understand why, in code below, $my_foo , $my_bar correctly inherited child class, if change $my_foo assigning reference $my_var, child class still sees 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: similar question: do extended classes inherit static var values (php)? mine more focused on inheritance , references.

edit2: please note point of question not late static binding, it's why, since $my_foo , $my_bar inherited, changing them in foo doesn't affect them when accessed in bar. , only happens references. in fact if change:

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

the behavior totally changes , last bar::bar_print_vars(); // outputs foo foo

foo different class bar. try calling bar::break_inheritance(); , see happens.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -