我試圖訪問它的子類中的父類屬性,但我不斷收到此錯誤致命錯誤:未定義類常量'臂'blabla.php 26行。 這是我下面的代碼blabla.php如何訪問擴展類中的父級PHP類屬性
blabla.php
<?php
class mother
{
public $legs;
public $arms;
public $eyes;
function say($arm,$eye,$leg)
{
$this->arms = 'pretty';
$this->eyes = 'stunning';
$this->legs = 'beautiful slim';
}
}
class daughter extends mother
{
public $newArms;
public $newEyes;
public $newLegs;
public function newSay()
{
$this->newArms = parent::arms;
$this->newEyes = parent::eyes;
$this->newLegs = parent::legsparent::arms;
echo 'I have a beautiful daughter who has '.$this->newArms.' arms, '.$this->newEyes.' eyes and '.$this->newLegs.' legs';
}
}
$baby = new daughter();
$baby->newSay();
?>
如果在我的代碼錯誤,請告訴我,如何糾正它。 在此先感謝。
您正在犯一個根本性錯誤。女兒不應該延長母親。一位母親有一個女兒,女兒是一個新的實體,不是母親的延伸。 – vascowhite