PHP inherits the constructor method, and the member variables are often vague about these concepts, which are summarized as follows :? 1. if the subclass does not have a constructor, the constructor of the parent class is called by default? 2. if the subclass has a constructor, the constructor of the parent class is not called and the member variables of the parent class are overwritten. if you want to call the construct ();? 3. Will the member variables and methods of the parent class inherit from the quilt class by default? Eg: c PHP inherited constructor, member variable
I found myself vague about these concepts, which are summarized as follows:
?
1. if the subclass does not have a constructor, the constructor of the parent class is called by default.
?
2. if the subclass has a constructor, the constructor of the parent class will not be called, and the member variables of the parent class will be overwritten. if you want to call the construct (), use parent :__ construct ();
?
3. the member variables and methods of the parent class inherit from the quilt class by default.
?
Eg:
class A{ public $a = array(); public $b = ''; public function __construct(){ $this->a = array('a', 'b', 'c'); $this->b = 'bobby'; echo 'construct' . "\n"; } public function get(){ print_r($this->a); } } class B extends A{ public function __construct(){ parent::__construct(); $this->c = 'CC'; } public function get(){ array_push($this->a, 'd'); print_r($this->a); echo $this->c; } public function getb(){ echo parent::$this->b; } } //$a = new A(); $b = new B(); $b->get(); $b->getb();