我一直在想下面一段时间: 我正在使用一种构造方式将一些(在本例中为'tool'-)类加载到'parent'或'controller'类的属性中。更具体的:引用'假人父母的属性......?
class a
{
public $b;
public $c;
public function __construct()
{
$this->b = new b();
$this->c = new c();
}
}
class b
{
// Properties and methods of b.
}
class c
{
// Properties and methods of c.
}
$a = new a();
a和b(和c)不是(分别)“父子”中的一个为之间的关系存在只能一个的实例。到目前为止好,但是:此刻我访问类的成员,类B或C通过得到的一个局部引用呼吁通过“全球” ......所以说:
class a
{
public $b;
public $c;
public $a_property = 'something something';
public function __construct()
{
$this->b = new b();
$this->c = new c();
}
}
class b
{
public function test()
{
global $a;
echo $a->a_property;
}
}
class c
{
// Properties and methods of c.
}
$a = new a();
$a->b->test();
这是一个:丑(我认为),b:(我找不到它的任何文件/参考了,但)我认为,作为PHP 5的下面应该是可能的(?):
class a
{
public $b;
public $c;
public $a_property = 'something something';
public function __construct()
{
$this->b = new b();
$this->c = new c();
}
}
class b
{
public function test()
{
echo $this->a_property;
}
}
class c
{
// Properties and methods of c.
}
$a = new a();
$a->b->test();
这并未没有工作...虽然服务器运行PHP 5.3.6 ...
奥卡姆的剃刀:我可能错误的功能...! ;-)
AlienWebguy嗨, 感谢您的回复!但是:我从来没有见过范围解析运算符和对象访问器组合使用...并且它不起作用...(以及变量'$'.. ??)另外:是,“a :: $ a_property“将工作,如果”$ a_property“被宣布为'静态'...这,我宁愿不要做... – IndigoDragon