在PHP中,子类可以通过使用parent::
关键字来调用父类的方法。
假设有一个父类ParentClass
和一个子类ChildClass
,子类想要调用父类的方法,可以使用以下语法:
class ParentClass {
public function parentMethod() {
echo "This is the parent method.";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
// 调用父类的方法
parent::parentMethod();
}
}
$childObj = new ChildClass();
$childObj->childMethod(); // 输出:This is the parent method.
在子类的方法中,使用parent::
关键字后面跟着父类的方法名,就可以调用父类的方法了。
上一篇:php如何去除数组的元素
下一篇:php如何设置缓存
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站