【发布时间】:2012-05-29 01:57:49
【问题描述】:
在这个例子中,如何从对象getContainerID() 中的getContainerID() 方法访问对象$containerObj 中的属性,或者至少获得指向$containerObj 的指针?
class Foo {
public $id = 123;
}
class Bar {
function getContainerID() {
... //**From here how can I can access the property in the container class Foo?**
}
}
$containerObj = new Foo();
$containerObj->bar = new Bar();
echo $containerObj->bar->getContainerID();
【问题讨论】:
-
按照惯例,您的课程应该是
Foo和Bar。像FOO和BAR这样完全大写的单词通常是为常量保留的。 -
你不能。除非
Bar包含指向其父对象的指针 (Foo)。但是,这样的数据结构似乎是使用两个单独的对象人为设计的。选择一个来暴露和封装另一个。
标签: php object properties containers