这是类的结构。我希望Observer:callme()也可以从Children中调用。
class Observer
{
protected callme()
{
}
}
class Parent extends Observer
{
function createChild()
{
$this->callme(); // this is OK
return new Child ($this);
}
}
class Child
{
private $this myParent;
public function __constructor ($myParent)
{
$this->myParent = $myParent;
}
public function __destroy()
{
$this->myParent->callme(); // FAIL!
}
}
那么如何让FAIL起作用?(不公开,因为它仅供“Parent”和其“Children”内部使用)
Child
不属于与Observer
和Parent
相同的类层级,因此它无法调用Observer::callme()
。你的类名表明你希望Parent
成为Child
的超类,但代码并非如此。 - Josh JParent
中实例化一个新的Child
对象并不会使得Child
成为Observer
的子类,因此受保护的方法callme()
在其中不可用。 - BenM