当前位置:网站首页>php继承(extends)
php继承(extends)
2022-07-02 05:47:00 【杰儿__er】
类的继承要通过extends来实现
class 子类名 extends 父类名 {…………}
子类访问父类中public成员方法
- 子类可以继承父类的构造函数,当子类被实例化时,php会先在子类中查找构造函数。
- 若子类有自己的构造函数,php会先调用子类中的构造函数。
- 当子类没有自己的构造函数时,php则会去调用父类中的构造函数。
<?php
class Website{
public $name, $url, $title;
public function __construct(){
echo '--基类中的构造函数--';
}
public function demo(){
echo '--基类中的成员方法';
}
}
class ClassOne extends Website{
}
class ClassTwo extends Website{
public function __construct(){
echo '--子类中的构造函数--';
}
}
$object = new ClassOne();
$object->demo();
/*结果为:--基类中的构造函数--
--基类中的成员方法*/
$object2 = new ClassTwo();
$object2->demo();
/*结果为:--子类中的构造函数--
--基类中的成员方法*/
?>
子类访问父类中protected成员方法
- 有些类继承的属性,并不想在类外都被访问,这个成员被声明为保护成员,用protected修饰。
- 受保护成员不可在类外部被访问到,但可以在子类的内部访问。
- 也就是说我们可以在子类中设置一个函数,来访问父类中这个受保护的成员。
<?php
class Website{
public $name, $url, $title;
public function __construct(){
echo '--基类中的构造函数--';
}
protected function demo(){
echo '--基类中的成员方法';
}
}
class ClassOne extends Website{
}
class ClassTwo extends Website{
public function __construct(){
echo '--子类中的构造函数--';
}
public function test(){
$this->demo();
}
}
$object = new ClassOne(); /*结果为:--基类中的构造函数--*/
//$object->demo(); //在子类中调用父类使用protected修饰的成员方法会报错
$object2 = new ClassTwo();
$object2->test();
/*结果为:--子类中的构造函数--
--基类中的成员方法- */
?>
子类访问父类中private成员方法
- private修饰私有成员
- 父类中的私有成员不会被子类继承。
- 因此不可被子类放问到。
<?php
class Website{
public $name, $url, $title;
public function __construct(){
echo '--基类中的构造函数--';
}
private function demo(){
echo '--基类中的成员方法';
}
}
class ClassTwo extends Website{
public function test(){
$this->demo();
}
}
$object2 = new ClassTwo();
$object2->test();
/*结果为: --基类中的构造函数--
PHP Fatal error: Uncaught Error: Call to private method Website::demo() from context 'ClassTwo' in 5_php_extends_private.php:14*/
/*出错原因: 子类中调用父类中使用private关键字修饰的成员,程序会报错并终止。*/
?>
边栏推荐
- Fabric. JS right click menu
- Alibaba: open source and self-developed liquid cooling data center technology
- Grbl software: basic knowledge of simple explanation
- ThreadLocal memory leak
- “簡單”的無限魔方
- Gee series: unit 9 generate sampling data in GEE [random sampling]
- Pytorch Chinese document
- 2022-2-14 learning xiangniuke project - Section 7 account setting
- 【技术随记-08】
- Fabric. JS iText set italics manually
猜你喜欢

Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?

Centos8 installation mysql8.0.22 tutorial

Technologists talk about open source: This is not just using love to generate electricity

Disable access to external entities in XML parsing

Grbl software: basic knowledge of simple explanation

死磕大屏UI,FineReport开发日记

Importation de studio visuel

Balsamiq wireframes free installation

Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import

6. Network - Foundation
随机推荐
Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
Installation du tutoriel MySQL 8.0.22 par centos8
With an amount of $50billion, amd completed the acquisition of Xilinx
centos8安裝mysql8.0.22教程
Zzuli:1062 greatest common divisor
Win10 copy files, save files... All need administrator permission, solution
Fabric. JS background is not affected by viewport transformation
A collection of commonly used plug-ins for idea development tools
Typora installation (no need to enter serial number)
【LeetCode】Day92-盛最多水的容器
The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market
Zzuli:1064 encrypted characters
死磕大屏UI,FineReport开发日记
Ubuntu 20.04 installing mysql8
Zzuli:1061 sequential output of digits
GRBL 软件:简单解释的基础知识
Simply encapsulate JS and apply it
Operator details
RGB 无限立方体(高级版)
[Chongqing Guangdong education] selected reading reference materials of British and American literature of Nanyang Normal University