以下示例定义了一个foo类,它构造一个bar类,并将其存储为属性$foo->bar
。在bar类中,是否可以引用'false'父类,并使用它的函数?
class bar
{
public function test_false_parent()
{
//Is it possible to access foo->display() from here
{unknown code}::display();
}
}
class foo
{
public $bar;
public function __construct()
{
$this->bar = new bar;
}
public function display()
{
echo "in";
}
}
$foo = new foo;
$foo->bar->test_false_parent();
//Equivalent to $foo->display();
答案 0 :(得分:5)
不是没有后退参考:
class bar
{
protected $foo;
public function __construct(foo $foo)
{
$this->foo = $foo;
}
public function test_false_parent()
{
$this->foo->display();
}
}
class foo
{
public $bar;
public function __construct()
{
$this->bar = new bar($this);
}
public function display()
{
echo "in";
}
}
$foo = new foo;
$foo->bar->test_false_parent();