为类实例创建静态getter:
class DD_Awesome_Plugin
{
/**
* Plugin main instance.
*
* @type object
*/
protected static $instance = NULL;
/**
* Access plugin instance. You can create further instances by calling
* the constructor directly.
*
* @wp-hook wp_loaded
* @return object T5_Spam_Block
*/
public static function get_instance()
{
if ( NULL === self::$instance )
self::$instance = new self;
return self::$instance;
}
public function add_menu_page()
{
add_options_page(
\'DD Awesome PLugin\',
\'DD Awesome PLugin\',
\'administrator\',
__FILE__,
array( $this, \'display_options_page\' )
);
}
}
现在,您可以通过以下方式获得插件实例:
add_action(\'admin_menu\', function() {
DD_Awesome_Plugin::get_instance()->add_menu_page();
});
或:
add_action(
\'admin_menu\',
array( DD_Awesome_Plugin::get_instance(), \'add_menu_page\' )
);