简体   繁体   中英

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

I have created a php class in my theme function.php

class myWidgetSetting {

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

}

$setting = new myWidgetSetting();

but when I call this class $setting->get_title() inside:

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

Do i need to register class to wordpress ??

I'm not expert on PHP but to use $setting variable as global you need to declare it as such.

Like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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