class - Reassigning $this in PHP/Laravel -
i have class extends class b
class extends b{ function home(){ $site = new sitescontroller($this); return $site->home(); } }
i want every variables in class availabe in sitescontroller class. dont want extend sitescontroller since everytime called re-initialized , there lot of repeated query calls. tried pass $this when class created. cant reinitialize $this in sitescontroller this.
class sitescontroller implements siteinterface { function __construct($data) { $this = $data; } }
is there anyway can make work?? thanks.
you can't "reassign" $this.
but can assign instance of property of sitescontroller, means can access public methods/properties of within sitescontroller instance.
class sitescontroller implements siteinterface { protected $a; public function __construct(a $data) { $this->a = $data; } public function dosomething() { return $this->a->publicmethodofa(); } }
this "dependency injection", , applying principle of composition rather inheritence
Comments
Post a Comment