PHP: Class extends problem “Call to private method … from context …”

孤街醉人 提交于 2019-12-10 16:45:04

问题


I have 3 classes in WordPress (the question itself is unrelated to it):

class WP_Widget

class Theme_Widget extends WP_Widget

class Specific_Widget extends Theme_Widget

Essentially Theme_Widget contains some extension functions to the basic WP_Widget.

Inside Specific_Widget I call one of Theme_Widget's methods:

class Specific_Widget {

    function __construct() {
         $this->some_method_that_belongs_to_Theme_Widget();
    }
}

When I instantiate Specific_Widget, PHP throws a fatal error as follows:

Fatal error: Call to private method Theme_Widget::some_method_that_belongs_to_Theme_Widget() from context 'Specific_Widget' in ...

Do you have an idea as to how I can resolve this? This is the first time I've received this error from PHP. Could it be derive from WordPress itself?


回答1:


You must declare your method protected, rather than private, if you wish child classes to be able to use it.



来源:https://stackoverflow.com/questions/3007020/php-class-extends-problem-call-to-private-method-from-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!