当前位置:网站首页>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关键字修饰的成员,程序会报错并终止。*/
?>
边栏推荐
- 【论文翻译】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
- Zzuli:1065 count the number of numeric characters
- 7. Eleven state sets of TCP
- Straighten elements (with transition animation)
- 2022-2-15 learning xiangniuke project - Section 8 check login status
- Win10 copy files, save files... All need administrator permission, solution
- Visual Studio导入
- Fabric. JS right click menu
- Technologists talk about open source: This is not just using love to generate electricity
- Zzuli: maximum Convention and minimum common multiple
猜你喜欢

15 C language advanced dynamic memory management

ThreadLocal memory leak

Centos8 installation mysql8.0.22 tutorial

6. Network - Foundation

RGB 无限立方体(高级版)

Cube magique infini "simple"

brew install * 失败,解决方法
![[golang syntax] be careful with the copy of slices](/img/5e/1c82c58940939b94d03377ebdc03e3.jpg)
[golang syntax] be careful with the copy of slices

正则表达式总结

Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
随机推荐
Common protocols and download paths of NR
Php/js cookie sharing across domains
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
Visual studio import
数据挖掘方向研究生常用网站
Fabric. JS basic brush
1037 Magic Coupon
Fabric. JS round brush
Win10 copy files, save files... All need administrator permission, solution
Gee series: unit 9 generate sampling data in GEE [random sampling]
5g market trend in 2020
2022-2-14 learning xiangniuke project - Section 7 account setting
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Fabric. JS iText superscript and subscript
Fabric. JS background is not affected by viewport transformation
460. LFU cache bidirectional linked list
Visual Studio導入
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
all3dp. All Arduino projects in com website (2022.7.1)
软件测试 - 概念篇