oop - Php, inheritance, late static binding, unexpected calling chain -
consider code:
class c { public function get() { echo 'c'; static::get(); } public function save() { self::get(); } } class b extends c { public function get() { echo 'b'; static::get(); } } class extends b { public function get() { echo 'a'; } } $x = new a(); $x->save();
it echoes ca
while expected cba
to work way want, reverse logic - save call static::get()
start @ top of inheritence tree; , use calls parent::get()
in each inherited class in tree (except base level) before echoing own output
class c { public function get() { echo 'c'; } public function save() { static::get(); } } class b extends c { public function get() { parent::get(); echo 'b'; } } class extends b { public function get() { parent::get(); echo 'a'; } } $x = new a(); $x->save();
Comments
Post a Comment