我的印象是,孩子類繼承父母的屬性。然而,下面,在B類中輸出爲null ......有人能告訴我如何從父類訪問屬性嗎?定義在PHP中,我們如何獲得父類變量?
$aClass = new A();
$aClass->init();
class A {
function init()
{
$this->something = 'thing';
echo $this->something; // thing
$bClass = new B();
$bClass->init();
}
}
class B extends A {
function init()
{
echo $this->something; // null - why isn't it "thing"?
}
}
您已經定義了類A兩次,而類B沒有......你確定這段代碼是正確的嗎? – Mchl 2013-04-06 18:21:20
你的意思是'B類延伸A'嗎? – Sam 2013-04-06 18:22:42
SOrry,這是一個錯字,我糾正了它...... – Andrew 2013-04-07 09:17:53