有没有办法遍历一个对象来获取父对象数据?对于"父对象",我不是指父类,而是字面上的对象.这里有一个例子,在javascripty世界:-):
$parent->test = "hello world!";
$parent->child = new B();
Run Code Online (Sandbox Code Playgroud)
如果我可以访问子对象中父节点的所有数据,那就太好了:
class B{
//I know this doesn't exists, but it's what I wanted do do
function B(){
$this->parent = $this->parent();
echo $this->parent->test; //it would ouput "hello world"
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的解决方案是将父对象传递给子对象(作为参考)或使父对象成为全局对象.你有更好的解决方案吗?
谢谢!