class Action{ public function __construct() { echo 'hello Action'; } } class IndexAction extends Action{ public function __construct() { echo 'hello IndexAction'; } } $test = new IndexAction; //output --- hello IndexAction
class IndexAction extends Action{ public function __construct() { parent::__construct(); echo 'hello IndexAction'; } }
這樣就可以將兩句話同時輸出,當然還有一種辦法就是在父類中調用子類的方法.
代碼如下:
class Action{ public function __construct() { if(method_exists($this,'hello')) { $this -> hello(); } echo 'hello Action'; } } class IndexAction extends Action{ public function hello() { echo 'hello IndexAction'; } }