繼承的簡單規則(就構造函數而言)如下:
- 如果子類沒有自己的構造,然後父構造函數用於
- 如果子類確實有自己的構造,父構造函數必須顯式調用
- 合同在父母和孩子之間的類適用於構造,太
基本上,這意味着,應用到你的代碼中,父類的構造應該被稱爲AUT omatically。你說的構造函數沒有被調用,所以你可能定義在子類的構造函數,在這種情況下,簡單的添加此聲明:
parent::__construct();
幾個例子
class Foo
{
protected $val = null;
public function __construct()
{
$this->val = 123;
}
}
class Bar extends Foo
{
public function test()
{
return $this->val;
}
}
$b = new Bar();
echo $b->test();
這將呼應123
,因爲Foo::__construct()
被自動調用。然而,如果我們改變Bar
了一下,然後更改的行爲:
class Bar extends Foo
{
protected $another = null;
public function __construct()
{
$this->another = __METHOD__;
}
public function test()
{
return $this->val;
}
}
$b = new Bar();
var_dump($b->test());//null
所以val
屬性未被設置。一個簡單的修正,但:
class Bar extends Foo
{
protected $another = null;
public function __construct()
{
$this->another = __METHOD__;
parent::__construct();//call here, explicitly
}
public function test()
{
return $this->val;
}
public function getMethod()
{
return $this->another;
}
}
$b = new Bar();
var_dump($b->test());//123
echo $b->getMethod();//echoes Bar::__construct
總而言之,設置屬性的PDO
實例不被認爲是最好的想法。檢查出依賴注入,控制的谷歌倒置和所有這些buzz詞。
另一種方法是使用延遲加載干將:
class Foo
{
$this->db = null;
protected function getDb()
{
if ($this->db === null)
$this->db = new PDO();//create when needed
return $this->db;
}
}
這樣,DB連接將在最後一刻,當依靠該連接上的代碼調用getDb
方法創建...
類'ex1()'沒有被它的子類'ex2()'實例化,也沒有調用父類的構造,因此不調用ex1()構造函數 –
如果你快樂,你知道它...語法錯誤 –