且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从对象中获取父对象

更新时间:2023-11-30 21:59:04

你不能.

object1 不是父级,而是容器.如果您想从 object2 访问 object1 函数,您必须有对 object1 的引用.

object1 isn't the parent, it's the container. If you want access to an object1 function from object2, you must have a reference to 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; 
   }
}

这就是你要找的吗?