PHP:类扩展了问题“从上下文调用到私有方法...”。 [英] PHP: Class extends problem "Call to private method ... from context ..."

查看:102
本文介绍了PHP:类扩展了问题“从上下文调用到私有方法...”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WordPress中有3个类(问题本身与它无关):

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

本质上是Theme_Widget包含对基本WP_Widget的一些扩展功能。

Essentially Theme_Widget contains some extension functions to the basic WP_Widget.

在Specific_Widget内部,我调用了Theme_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();
    }
}

当我实例化Specific_Widget时,PHP抛出致命错误,因为如下:

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 ...

您是否知道如何解决此问题?这是我第一次从PHP收到此错误。

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?

推荐答案

您必须声明您的方法受保护的,而不是 private ,如果您希望子类能够使用它的话。

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

这篇关于PHP:类扩展了问题“从上下文调用到私有方法...”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆