【问题标题】:How can I replace a child class in PHP?如何替换 PHP 中的子类?
【发布时间】:2015-12-08 06:12:16
【问题描述】:

我有一个这样的子类:

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

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

这不起作用:

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

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

我该怎么做?

【问题讨论】:

  • 怎么不工作?如何调用类方法?

标签: php wordpress function class extends


【解决方案1】:

@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

【讨论】:

    猜你喜欢
    • 2013-05-16
    • 2014-12-17
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2011-12-30
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多