此外,在更为面向对象的方法中,您可以通过以下方式完成:
在插件类中:
class MyPlugin {
private $file_base;
/*
* Useful in case you put a lot of stuff in this class that
* could be used later from other classes without knowing if the
* class has already been instantiated. If it\'s just for the
* initialization without further using, go with a simple
* constructor
*/
public static function get_instance(){
static $plugin;
if ( !isset( $plugin ) ){
$plugin = new MyPlugin();
}
return $plugin;
}
/*
* If you go with a singleton, make it private, public otherwise
*/
private function __construct() {
$this->file_base = plugin_dir_path( dirname( __FILE__ ) ) . \'your_plugin_main_file.php\';
$this->init();
}
/*
* Should be invoked just from the constructor, so private
*/
private function init() {
register_activation_hook( $this->file_base, array( $this, \'install\' ) );
}
/*
* This is the callback, so make it public
*/
public function install() {
echo "little lamb, who made thee?";
}
}
然后在插件主文件(例如:your\\u plugin\\u main\\u file.php)中,只需:
MyPlugin::get_instance();