i'm trying access function of object child class, object protected variable of parent.
i'm not entirely sure best way go this... or pointers appreciated.
below how have setup now, it's not working. gives following error:
catchable fatal error: argument 1 passed app\parent::__construct() must instance of app\object, none given, called in controller.php on line 25 , defined in parent.php on line 12
so understand error, need somehow pass instance of parent class child class. seems anti-pattern because it's extending parent class. must missing basic.
parent.php
class parent { protected $object; public function __construct(object $object) // line 12 { $this->object = $object; } }
child.php
class child extends parent { public function dostuff() { return parent::$object->objectfunction()); } }
controller.php
... namespaces etc ... public function control() { $parent = new parent(new object($variable)); $child = new child(); // line 25 $child->dostuff(); }
dont instantiate seperate parent class, instantiated part of instantiating child class.
also pass object child instantiation , create __construct() method , pass parameter on it.
class child extends parent { public __construct($var) { parent::__construct($var); } public function dostuff() { return parent::$object->objectfunction()); } }
controller.php
public function control() { //$parent = new parent(new object($variable)); $child = new child(new object($variable)); // line 25 $child->dostuff(); }
Comments
Post a Comment