This article mainly summarizes several pointsphp object-orientedA few points when inheriting:
//people.class.php class People{ private $name; private $sex; private $birthday; private function construct($name='',$sex='01',$birthday='1999-01-01'){ echo 'people---construct<br>'; $this->name = $name; $this->sex = $sex; $this->birthday = $birthday; } public function get($key){ return $this->$key; } public function set($value,$key){ $this->$key = $value; } public function show(){ return 'people---'; } }
//student.class.php class Student extends People{ private $s_num; private $s_class; public function construct($name,$sex,$birthday,$num,$class){ //parent::construct($name,$sex,$birthday); echo 'Student--construct<br>'; $this->name = $name; $this->sex = $sex; $this->birthday = $birthday; $this->s_num = $num; $this->s_class = $class; } public function showInfo(){ return 'sutdent---'.$this->name.'----bir='.$this->birthday .'num=='.$this->s_num.'----class=='.$this->s_class; } }
The above two classes Student inherit the People class
The constructor method of the parent class is private, which means in java This class cannot be inherited, but in php, this class can be inherited, but one thing is that the constructor of the parent class cannot be called in the subclass Student
parent::construct($name,$sex,$birthday);
Otherwise, an error will be reported, and if the constructor of the parent class is private, then the subclass must have its own constructor and must be written clearly, otherwise Inheritance is not possible.
At the same time, what is different from java is that when a subclass inherits a parent class and the subclass has its own constructor, the parent The constructor of a class will not be executed unless called from the constructor of a subclass.
##
The above is the detailed content of Summary of several aspects of object-oriented inheritance in php. For more information, please follow other related articles on the PHP Chinese website!