PHP中怎么引用父类方法

   2024-11-04 9200
核心提示:在PHP中,可以使用parent::关键字来引用父类方法。例如,在子类中调用父类的方法可以这样写:class ParentClass {public functio

在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()方法。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号