1.12 Inheritance
1.12.1 Introduction inheritance
- Inheritance makes code hierarchical
- Subclass inherits properties and methods of the parent class, to achieve the reusability of code.
- Use the keyword extends inheritance
- Parent class and subclass are relative
grammar
class 子类 extends 父类{
}
example
<?php
//父类
class Person {
public function show() {
echo '这是人类<br>';
}
}
//子类继承父类
class Student extends Person {
}
//测试
$stu=new Student;
$stu->show(); //这是人类
Implementation process:
Step One: Find Student class show (), if you call to find, can not find it to locate the parent class
Step two: Query show the Person class ()
1.12.2 subclass call the parent class members
<?php
//父类
class Person {
public function show() {
echo '这是人类<br>';
}
}
//子类
class Student extends Person {
public function test() {
//方法一;
/*
$person=new Person();
$person->show(); //这是人类
*/
//方法二
$this->show(); //这是人类
}
}
//测试
$stu=new Student;
$stu->test();
summary:
1, a method of: calling a member of the parent class's parent class by instantiating
2. Method Two: Call to a member of the parent class by $ this keyword
1.12.3 protected
protected: protected, use the entire inheritance chain
example:
//例题一:
<?php
class A {
protected $num=10; //在整个继承链上访问
}
class B extends A {
public function getNum() {
echo $this->num;
}
}
//测试
$obj=new B(); //整个继承链上有A和B
$obj->getNum(); //10
//例题二:
<?php
class A {
public function getNum() {
echo $this->num;
}
}
class B extends A {
protected $num=10;
}
//测试
$obj=new B(); //整个继承链上有A和B
$obj->getNum(); //10
//例题三:
<?php
class A {
public function getNum() {
echo $this->num;
}
}
class B extends A {
protected $num=10;
}
//测试
$obj=new A(); //整个继承链上只有A
$obj->getNum(); //Notice: Undefined property: A::$num
1.12.4 inheritance constructor
rule:
1、如果子类有构造函数就调用子类的,如果子类没有就调用父类的构造函数。
2、子类的构造函数调用后,默认不再调用父类的构造函数
Call the constructor of the parent class by class name
类名::__construct()
example
<?php
class Person {
//父类的构造函数
public function __construct() {
echo '这是父类<br>';
}
}
class Student extends Person {
//子类的构造函数
public function __construct() {
Person::__construct(); //通过父类的名字调用父类的构造函数
parent::__construct(); //parent表示父类的名字
echo '这是子类<br>';
}
}
//测试
new Student();
Note: parent keyword indicates the name of the parent class, you can reduce coupling procedure
Example: passing parameters to the parent class
<?php
class Person {
protected $name;
protected $sex;
//父类的构造函数
public function __construct($name,$sex) {
$this->name=$name;
$this->sex=$sex;
}
}
class Student extends Person {
private $score;
//子类的构造函数
public function __construct($name,$sex,$score) {
parent::__construct($name,$sex); //调用父类构造函数并传递参数
$this->score=$score;
}
//显示信息
public function getInfo() {
echo "姓名:{$this->name}<br>";
echo "性别:{$this->sex}<br>";
echo "成绩:{$this->score}";
}
}
//测试
$stu=new Student('tom','男',88);
$stu->getInfo();
/*
姓名:tom
性别:男
成绩:88
*/
1.12.5 $ this Detailed
an address stored in this current object
<?php
class A {
public function __construct() {
var_dump($this);
}
}
class B extends A {
}
new A(); //object(A)#1 (0) { }
echo '<br>';
new B(); //object(B)#1 (0) { }
1.12.6 multiple inheritance
PHP does not allow multiple inheritance, multiple inheritance because prone to ambiguity
C inheritance how A and B, using inheritance chain