如何在PHP中替换子类?

时间:2015-12-08 06:12:17

标签: php wordpress function class extends

我有一个这样的子课:

class Child1 extends Parent {
    public function theFunction () {
        does some stuff...
    }
}

我想要用新的子类theFunction()替换Child1或整个班级Child2

这不起作用:

class Child2 extends Child1{
    public function theFunction () {
        does some stuff...
    }
}

Parent和Child1类在Wordpress插件中,因此我无法在不更改核心文件的情况下修改这些部分。我不想这样做。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

@Alexander Elgin是对的。

问题中的继承登录应该有效。例如:

<?php

class Parent1 {
}

class Child1 extends Parent1 {
    public function theFunction () {
        return 'Child1';
    }
}

class Child2 extends Child1{
    public function theFunction () {
        return 'Child2';
    }
}

$child1 = new Child1();
echo "child1 call: '{$child1->theFunction()}' \n";

$child2 = new Child2();
echo "child2 call: '{$child2->theFunction()}' \n";
?>

演示:https://eval.in/481992