简体   繁体   English

WordPress-致命错误:调用类时调用成员函数

[英]WordPress - Fatal error: Call to a member function when calling class

I have created a php class in my theme function.php 我在主题函数function.php中创建了一个php类

class myWidgetSetting {

    function get_title(){
     //some blah blah code
    }

}

$setting = new myWidgetSetting();

but when I call this class $setting->get_title() inside: 但是当我在内部调用此类$setting->get_title()

class myWidget extends WP_Widget { 
    function renderLayout(){
    global $setting;
            $setting->get_title();
    }
}

Then I get this error: 然后我得到这个错误:

Fatal error: Call to a member function get_title() on a non-object in D:\\wamp\\www\\wp-content\\themes\\twentyten\\widgets\\myWidget.php on line 3 致命错误:在第3行的D:\\ wamp \\ www \\ wp-content \\ themes \\ twentyten \\ widgets \\ myWidget.php中的非对象上调用成员函数get_title()

Do i need to register class to wordpress ?? 我是否需要注册课程以使用wordpress?

I'm not expert on PHP but to use $setting variable as global you need to declare it as such. 我不是PHP方面的专家,但是要使用$setting变量作为全局变量,您需要这样声明它。

Like this: 像这样:

global $setting;
$setting = new myWidgetSetting();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM