PHP中怎么引用父类方法
lewis
2016-12-18
22次阅读
在PHP中,可以使用parent::关键字来引用父类方法。例如,在子类中调用父类的方法可以这样写:
class ParentClass {
public function parentMethod() {
echo "This is parent method";
}
}
class ChildClass extends ParentClass {
public function childMethod() {
parent::parentMethod();
}
}
$child = new ChildClass();
$child->childMethod(); //输出:This is parent method
在子类的方法中使用parent::parentMethod()来调用父类的parentMethod()方法。

发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。