class hoge { public function A(){ echo "call A\n"; } private $i_ = 1; } class foo extends hoge { public function B(){ echo "call B\n"; } private $j_ = 999; } $hoge_obj = new hoge(); $hoge_obj->A(); var_dump($hoge_obj); $foo_obj = new foo(); $foo_obj->B(); $foo_obj->A(); var_dump($foo_obj);
### オーバーライド
class hoge { public function A(){ echo "call A\n"; } private $i_ = 1; } class foo extends hoge { public function A(){ echo "call B\n"; } private $j_ = 999; }
– protected
class hoge { private function h_pri_func(){ echo "call h_pri_func\n"; } protected function h_pro_func(){ echo "call h_pro_func\n"; } } class foo extends hoge { public function f_pri_func(){ echo "call f_pri_func\n"; } public function test(){ $this->f_pri_func(); $this->h_pro_func(); } } $foo_obj = new foo(); $foo_obj->test();
– parent: 継承元のconstructor, destructorを呼ぶ
class foo extends hoge { public function __construct(){ parent::__construct(); echo "run foo constructor\n"; } public function __destruct(){ parent::__destruct(); echo "run foo destructor\n"; } }
なるほど、constructorの整理にはなった。