你不能。
object1不是父级,它是容器。如果object1要从访问函数object2,则必须具有对 的引用object1。
使用这种模式:
class class1
{
public $child;
public function __construct()
{
$this->child = new class2($this);
}
}
class class2
{
private $parent;
public function __construct(class1 $parent)
{
$this->parent = $parent;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是你要找的吗?