当我使用它返回NULL
父类的属性,我不知道为什么会这样,示例代码:无法访问父类的属性
class Foo
{
public $example_property;
public function __construct(){
$this->example_property = $this->get_data();
}
public function get_data() {
return 22; // this is processed dynamically.
}
}
class Bar extends Foo
{
public function __construct(){}
public function Some_method() {
return $this->example_property; // Outputs NULL
}
}
其实,当我设置属性值与constructor
发生,但如果我staticly设定值(例如:public $example_property = 22
,它不会返回任何NULL
更
这是为我工作。您能否使用您用来获取该财产的代码编辑帖子? – fpietka
我用@u_mulder的答案,它的工作! – Amin
所以你必须在'Bar'类中有'__construct()',否则它会被继承。 – fpietka