oop - return the extending class instance from an abstract method in PHP -


i have class extends abstract class. php allows access instance of extending class within abstract methods?

something like:

abstract class foo{     protected function bar(){         return $this;     } }  class bar extends foo{     public function foo(){         // should hold bar instance , not foo's         $barclassinstance = $this->bar();     } } 

where $barclassinstance hold bar class instance, instead of abstract foo instance?

trying out worth thousand stackoverflow questions

<?php  abstract class foo{     protected function bar(){         echo 'foo', php_eol;         var_dump($this);         return $this;     } }  class bar extends foo{     public function foo(){         echo 'bar', php_eol;         var_dump($this);         // should hold bar instance , not foo's         $barclassinstance = $this->bar();         var_dump($barclassinstance);     } }  $bar = new bar(); $bar->foo(); 

output https://3v4l.org/b73bt

bar object(bar)#1 (0) { } foo object(bar)#1 (0) { } object(bar)#1 (0) { } 

$this reference instance regardless of subclass instance of. there no foo instance because foo cannot instantiated, abstract. if foo concrete class, wouldn't have foo $this , bar $this in same object. have $this pointing specific subclass created.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -