PHP重写非';我没有直接打电话

PHP重写非';我没有直接打电话,php,Php,我有两个班:家长班和孩子班: class Post { public function save() { $this->update_data(); $this->custom_updates(); $this->load(); } protected function custom_updates() { /* empty */ } } class Event extends Post {

我有两个班:家长班和孩子班:

class Post
{
    public function save()
    {
        $this->update_data();
        $this->custom_updates();
        $this->load();
    }

    protected function custom_updates() { /* empty */ }
}
class Event extends Post
{
    protected function custom_updates()
    {
        //perform custom updates specific for Event objects
    }
}

$Event = new Event();
$Event->save();
当我执行上述代码时,将从原始Post类而不是子事件类调用custom_updates()方法。有没有聪明的方法重写父方法,以便对象使用它?或者,唯一的选项是修改正在调用的方法,在本例中为save()


我看不出你的代码有任何问题,一切都应该正常,试着在你的方法中做一些
var_dump
,看看输出是什么“当我执行上面的代码时,方法
custom_updates()
将从原始
Post
类调用,而不是子
事件
类。”--这是不正确的。调用
$this
类中的方法
custom\u updates()
。查看:这就是继承在PHP中的工作方式。您发布的代码覆盖了父方法。这是一个基本的oop原则。请再次检查。调用的方法将是重写的子方法-我的错,我以前测试过,但它不起作用。现在,当我再试一次时,它工作得非常好。我的错误,我为这个愚蠢的问题道歉。
class Event extends Post
{
    public function save()
    {
        $this->update_data();
        $this->custom_updates();
        $this->load();
    }

    protected function custom_updates()
    {
        //perform custom updates specific for Event objects
    }
}