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

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 -