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