當我使用它返回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