【问题标题】:How to call the child class method in parent class in php [duplicate]如何在php中调用父类中的子类方法[重复]
【发布时间】:2017-06-26 17:38:30
【问题描述】:
<?php

class mainClass
{
    public function demo()
    {
        return "You have entertered to demo1";
    }

}

class childClass extends mainClass
{
    public function demo2()
    {
        return $this->demo();
    }
    public function demo3()
    {
        return "You have entered to demo3";
    }
}

我想在mainClass中调用childClassdemo3方法。因为我是新手,所以我有点困惑。我也在为此考虑抽象类。但是我对抽象类的了解还不够。

【问题讨论】:

  • 您为什么要这样做?只需将 demo3 放在父级中即可。

标签: php oop


【解决方案1】:

这就是你要找的吗?您不必调用 mainClass,因为您通过新的 childClass 扩展它。我还在 demo3 中添加了 echo 以在屏幕上显示结果。

<?php

class mainClass
{
    public function demo()
    {
        return "You have entertered to demo1";
    }

}

class childClass extends mainClass
{
    public function demo2()
    {
        return $this->demo();
    }
    public function demo3()
    {
        echo "You have entered to demo3";
    }
}
$test = new childClass;
$test->demo3();

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-09
    • 2016-02-14
    • 2023-02-26
    • 2020-04-26
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多