当前位置:网站首页>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关键字修饰的成员,程序会报错并终止。*/
?>
边栏推荐
- 2022-2-14 learning xiangniuke project - Section 6 displays login information
- Fabric. JS 3 APIs to set canvas width and height
- idea开发工具常用的插件合集汇总
- all3dp.com网站中全部Arduino项目(2022.7.1)
- OLED12864 液晶屏
- mysql事务和隔离级别
- 死磕大屏UI,FineReport开发日记
- Fabric. JS iText superscript and subscript
- 中小型项目手撸过滤器实现认证与授权
- Fabric. JS iText set italics manually
猜你喜欢
【技术随记-08】
Alibaba: open source and self-developed liquid cooling data center technology
Gee series: unit 9 generate sampling data in GEE [random sampling]
Installation du tutoriel MySQL 8.0.22 par centos8
Vite打包后的dist不能直接在浏览器打开吗
brew install * 失败,解决方法
Appnuim environment configuration and basic knowledge
Straighten elements (with transition animation)
Visual Studio导入
Grbl software: basic knowledge of simple explanation
随机推荐
Storage of data
生成二维码
Get the details of the next largest number
Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
Usage record of vector
2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
all3dp. All Arduino projects in com website (2022.7.1)
GRBL 软件:简单解释的基础知识
460. LFU cache bidirectional linked list
小程序跳装到公众号
Practice C language advanced address book design
Brew install * failed, solution
【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
6. Network - Foundation
Youth training camp -- database operation project
2022-2-14 learning xiangniuke project - Section 7 account setting
Fabric. JS basic brush
1037 Magic Coupon
15 C language advanced dynamic memory management
Fabric. JS right click menu