我有下面的代码,我希望返回“已工作”,但什么也不返回。
class Foo {
public function __construct() {
echo('Foo::__construct()<br />');
}
public function start() {
echo('Foo::start()<br />');
$this->bar = new Bar();
$this->anotherBar = new AnotherBar();
}
}
class Bar extends Foo {
public function test() {
echo('Bar::test()<br />');
return 'WORKED';
}
}
class AnotherBar extends Foo {
public function __construct() {
echo('AnotherBar::__construct()<br />');
echo($this->bar->test());
}
}
$foo = new Foo();
$foo->start();路由器
Foo::__construct() <- From $foo = new Foo();
Foo::start() <- From Foo::__construct();
Foo::__construct() <- From $this->bar = new Bar();
AnotherBar::__construct() <- From $this->anotherBar = new AnotherBar();因为我从$bar类定义了Foo,并且将AnotherBar扩展到Foo,所以我希望从Foo中获得已经定义的变量。
我看不出是怎么回事。我从哪里开始?
谢谢!
发布于 2012-06-09 01:27:44
AnotherBar实例从未调用过它的start方法,因此它的$this->bar是未定义的。
在显示错误时,您将得到以下消息:
注意:未定义的属性: AnotherBar::$bar in - on第20行,致命错误:调用非对象在第20行中的成员函数test()
您可以在<?php行之后包含以下代码以查看所有错误:
ini_set('display_errors', 'on');
error_reporting(E_ALL);当然,您也可以通过php.ini实现这一点,这将是一个更干净的解决方案。
https://stackoverflow.com/questions/10957682
复制相似问题