Linux 拨号vps windows公众号手机端

PHP中怎么引用父类方法

lewis 9年前 (2016-12-18) 阅读数 6 #程序编程
文章标签 PHP

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

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

热门