我试图从子类访问一个对象的函数,其中对象是父类的受保护变量。访问作为父类的成员变量的对象的成员函数
我不完全确定最好的方式去... ...任何帮助或指针,将不胜感激。
下面是我如何安装它,但它不工作。它提供了以下错误:
Catchable fatal error: Argument 1 passed to App\Parent::__construct() must be an instance of App\Object, none given, called in Controller.php on line 25 and defined in Parent.php on line 12
所以我理解错误,我需要在父类的实例某种方式传递到子类。但是这看起来像是反模式,因为它扩展了Parent类。我必须错过一些基本的东西。
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();
}
谢谢,更有道理,它的工作吧!我会尽快接受答案。 –