擴展構造方法:如何從我在我的用戶類創建的構造方法,父類
class User {
protected $name;
protected $title;
public function __construct($name = null, $title = null) {
$this->name = $name;
$this->title = $title;
}
}
現在,我想我的客戶端類內延伸構造方法。在用多個代碼塊「試驗」之後,我似乎無法弄清楚如何做到這一點。我希望$ company包含在初始化中。下面是最新版本的我不成功的代碼:
class Client extends User
{
protected $company;
public function __construct($company = null)
{
parent::__construct($name = null, $title = null);
$this->company = $company;
}
public function getCompany()
{
return $this->company;
}
public function setCompany($company)
{
$this->company = $company;
}
}
$MyPHPClassSkillLevel = newbie;
我真的只是皮毛,所以任何的幫助深表感謝擴展類的表面。謝謝。
它在做什麼或不做什麼?它看起來至少應該起作用,也許不是你想要的,但我們不知道。 – AbraCadaver
'Client'構造函數應該可以接受'$ name'和'$ title'參數,所以它可以在構造父項時傳遞它們。 – Barmar
噢,別忘了:爲了開發一個好的OOP解決方案,您需要對所謂的** S.O.L.I.D有一個很好的理解。原則**!還有更多(DRY,KISS等),但是SOLID是OOP的核心。 – 2017-06-16 16:48:02