Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在主类中调用子方法_Php - Fatal编程技术网

Php 在主类中调用子方法

Php 在主类中调用子方法,php,Php,我想在不声明新的Child()的情况下调用主类中的Child方法 最好的祝愿将类设置为抽象,并将要从子类调用的方法声明为抽象 abstract class main { function __construct() { } public function test() { return "test"; } abstract function childmethod(); } class child extends main {

我想在不声明新的Child()的情况下调用主类中的Child方法


最好的祝愿

将类设置为
抽象
,并将要从子类调用的方法声明为
抽象

abstract class main {
    function __construct() {

    }
    public function test() {
          return "test";
    }

  abstract function childmethod();
 }

class child extends main {
    function __construct() {

    }
    function childmethod() {
        return "childmethod";
    }
}

$main = new child();
echo $main->childmethod();// echoes childmethod
echo $main->test();// echoes test

如果我正确理解了你的要求,你就做不到,这是项目理念中的一个错误。 如果需要在父级中调用子方法,这意味着该方法应该在父级中调用


如果不实例化对象,就无法调用对象的方法,除非您创建一个静态方法

它的
扩展
,而不是@Uchiha在
解析错误中所说的
扩展
:…
我刚刚从OP的问题中复制过来。谢谢:)这行不通。您需要实例化一个
new child()。现在我想把它分成每个模态/区域的和平。例如:API-Refs-News-bla这就是为什么我有这个问题,我理解。但是,当您发现父级中需要一个方法时,最好的方法是在父级中移动该方法
abstract class main {
    function __construct() {

    }
    public function test() {
          return "test";
    }

  abstract function childmethod();
 }

class child extends main {
    function __construct() {

    }
    function childmethod() {
        return "childmethod";
    }
}

$main = new child();
echo $main->childmethod();// echoes childmethod
echo $main->test();// echoes test