如何为子方法添加更多功能?

时间:2015-03-10 10:18:25

标签: php class inheritance methods extend

    class Parent_class{
    function parent_says(){
        echo "HI. I'm parent!";
    }
}

class Child_class extends Parent_class{
    function parent_says(){
        //I want it to also says "HI. I'm parent!", that's inside parent method.
        echo "I say hello!";
    }
}

$Child_class = new Child_class;
$Child_class->parent_says();

1 个答案:

答案 0 :(得分:3)

首先使用parent::调用父类的方法。 E.g。

class Child_class extends Parent_class{
    function parent_says(){
        parent::parent_says();
        echo "I say hello!";
    }
}

有关详细信息,请参阅here