Inherited:
One of the greatest advantages of object-oriented programming is that one class can inherit and have a member variable and member method of another existing class, where the inherited class is called the parent class, and the inherited class is called a subclass. The subclass inherits the parent class and obtains all the member variables and member methods of the parent class. Inheritance can improve the reusability of code and maintainability.
Through the inheritance of classes, subclasses can not only have newly defined member variables and member methods, but also have all member variables and member methods of the parent class. The syntax format is as follows:
class
子类名称
extends
父类名称 {
//子类的成员变量列表
function
成员方法1([参数1,参数2,……]){
//成员方法1的具体实现
}
function
成员方法2([参数1,参数2,……]){
//成员方法2的具体实现
}
//其他成员方法
}
|
The following creates a student class that inherits from the already existing people class, with the following code:
class
People{
function __construct(
$name
,
$age
){
$this
->name=
$name
;
$this
->age=
$age
;
}
function
say(){
echo
"姓名:"
.
$this
->name.
" "
;
echo
"年龄:"
.
$this
->age;
echo
" "
;
}
function
__destruct(){}
}
class
Student
extends
People {
function
study(
$lang
){
echo
"我正在整理"
.
$lang
.
"……"
;
}
}
$student
=
new
Student(
"大兵"
,25);
$student
->say();
$student
->study(
"PHP基础知识"
);
|
The result of the code execution is:
Name: Soldier Age: 25
I'm working on the basics of PHP ...
Parent:: keyword
Keyword Parent:: Used to represent the parent class and the member method in the parent class to invoke, the syntax is as follows:
parent::父类的成员方法([参数1,参数2,……]) |
The following example uses the parent:: keyword to invoke the constructor method in people, the code is as follows:
class
People{
function
__construct(
$name
){
$this
->name=
$name
;
}
function
__destruct(){}
}
class
Student
extends
People {
function
__construct(
$name
,
$age
){
parent::__construct(
$name
);
$this
->age=
$age
;
}
function
say(){
echo
"姓名:"
.
$this
->name.
" "
;
echo
"年龄:"
.
$this
->age;
echo
" "
;
}
function study(
$lang
){
echo
"我正在整理"
.
$lang
.
"……"
;
}
function
__destruct(){}
}
$student
=
new
Student(
"大兵"
,25);
$student
->say();
$student
->study(
"PHP基础知识"
);
|
The result of the code execution is:
Name: Soldier Age: 25
I'm working on the basics of PHP ...
instanceof operator
Detects whether the current object instance belongs to a class you can use the instanceof operator, which returns a Boolean value with the following syntax:
overriding the parent class method
Methods that inherit from the parent class can be overridden in a subclass, also called methods, as needed. The overriding method and the overridden method require the same method name, parameter list, and return value type for method overrides.
The following is an example of a say () method that implements a subclass student overriding the parent class people, with the following code:
class
People{
function
__construct(
$name
,
$age
){
$this
->name=
$name
;
$this
->age=
$age
;
}
function
say(){
echo
"姓名:"
.
$this
->name.
" "
;
echo
"年龄:"
.
$this
->age;
echo " "
;
}
function
__destruct(){}
}
class
Student
extends
People {
function
say(
$lang
){
echo
$this
->name.
"正在整理"
.
$lang
.
"……"
;
}
}
$student
=
new
Student(
"大兵"
,25);
$student
->say(
"PHP基础知识"
);
|
The result of the code execution is:
The Marines are tidying up PHP basics ...
PHP classes and objects: inheritance