如何使基类中的方法调用与PHP中的基类保持一致?

如何使基类中的方法调用与PHP中的基类保持一致?,php,inheritance,Php,Inheritance,但是在parent中还有另一种方法: class son extends parent { ... public function func_name() { //do some additional stuff here ... parent:func_name(); } } 例如: class parent { ... public another_func() { $this

但是在
parent
中还有另一种方法:

class son extends parent {
    ...
    public function func_name()
    {
        //do some additional stuff here
        ...
        parent:func_name();
    }

}
例如:

class parent {
...
    public another_func()
    {
        $this->func_name();//how to stick to the one in parent here???
    }
}

重命名
parent::func\u name
并将其设置为私有。从
parent::other_func
(可能从
parent::func\u name
的新实现中)调用该函数。

或者
ParentClassName::func\u name
不起作用?

这看起来像是硬编码,有更自动化的解决方案吗?您会发现非常特别的事情,因此解决方案是非常定制的。OOP概念不打算以这种方式使用。公共方法应该是用于外部的API,而不是用于某些内部工作。在您的情况下,您应该将“func_name”设置为私有,因为它是实用函数,而不是API。不,必须通过实例调用。阅读您对此的评论,然后阅读您在下面写的评论,听起来您好像在试图越狱和欺骗OOP结构的设计方式(它是故意这样设计的)-我认为是时候重新考虑这一特定部分的设计并采用不同的设计了。只是一个想法。祝你好运,那又怎样?我的解决方案没有将
parent::func_name
设置为私有。它重命名
parent::func_name
,并使重命名后的函数成为私有函数。如果您确实需要一个公共的
parent::func_名称
,那么您可以自由实现一个,因为
parent::func_名称
已重命名,因此不再存在。
$inst = new son;
$inst->another_func()////how to make the func_name within another_func stick to the one in parent???
public another_func () {
    if (get_class($this) == 'parent') {
        $this->func_name(); // $this is an instance of parent object
    } else {
        parent::func_name(); // $this is an instance of some child class
    }
}