論WordPress的單例模式,和更好的初始化方法
WordPress中,插件使用單例模式,性能更好,免得資源浪費。
平時,以面向對象的寫法是這樣的:
class Plugin
{
/**
* Constructor.
*/
public function __construct()
{
// Actions
add_action('wp_enqueue_scripts', array($this, 'enqueue_script'));
// Filters
add_filter('the_content', array($this, 'the_content'));
}
/**
* 一些方法
*/
public function enqueue_script()
{
// ...
}
}
$plugin = new Plugin();
或者,其他流行的方法是實例化一個對象類。在外部,使用該實例注冊你的動作和過濾器。
class Plugin
{
public function enqueue_script()
{
// ...
}
public function the_content($content)
{
// ...
return $content;
}
}
$plugin = new WP_Kickass_Plugin();
// Actions
add_action('wp_enqueue_scripts', array($plugin, 'enqueue_script'));
// Filters
add_filter('the_content', array($plugin, 'the_content'));
這兩種方法都有一個問題,那就是沒有一種簡單的方法可以在之后將該對象取回,容易被覆蓋。當然了,名字可以盡量起得不一樣。
實際上,WordPress同樣可以使用單例模式,實現單例模式的類有三個元素。你必須:
- 有一個私有靜態(tài)變量來存儲實例化的對象。
- 將構造函數設為私有。
- 創(chuàng)建一個公共靜態(tài)方法(通常稱為“get_instance”)來獲取實例化的對象。
看一下前面的兩個例子。可以按照上述步驟將它們轉換為單例。生成的代碼與原始代碼非常接近。第一個示例使用構造函數注冊所有操作和過濾器,可以在下面看到轉換后的代碼。
class Plugin
{
private static $instance;
public static function get_instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
private function __construct()
{
// Actions
add_action('wp_enqueue_scripts', array($this, 'enqueue_script'));
// Filters
add_filter('the_content', array($this, 'the_content'));
}
/**
*加載其他功能
*/
public function enqueue_script()
{
// ...
}
public function the_content($content)
{
// ...
return $content;
}
}
$plugin = Plugin::get_instance();
將其轉換為單例需要進行一些更改。將構造函數設為私有,并添加了靜態(tài)變量和“get_instance”方法,結果與前面的示例幾乎相同。
同樣,可以放到外面來調用:
class Plugin
{
private static $instance;
public static function get_instance()
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
private function __construct()
{
// So ronery...
}
public function the_content($content)
{
// ...
return $content;
}
}
// Actions
add_action('wp_enqueue_scripts', array(Plugin::get_instance(), 'enqueue_script'));
// Filters
add_filter('the_content', array(Plugin::get_instance(), 'the_content'));
我們需要創(chuàng)建一個空的私有構造函數。這是因為默認情況下構造函數是公共的。私有構造函數的另一個副作用是我們不能使用new關鍵字創(chuàng)建對象。我們需要通過調用“get_instance”將其傳遞給寄存器函數。值得注意的是,單例使第二種方法的吸引力大大降低。如果在構造函數中注冊鉤子,那么單例模式更有趣。
從構造函數中初始化插件
構造函數的作用是準備對象以供使用,用插件API注冊操作和過濾器根本不適合這個任務。使用靜態(tài)方法創(chuàng)建對象并在那里注冊所有內容顯然更好。
class Plugin
{
/**
* 注冊插件
*/
public static function register()
{
$plugin = new self();
// Actions
add_action('wp_enqueue_scripts', array($plugin, 'enqueue_script'));
// Filters
add_filter('the_content', array($plugin, 'the_content'));
}
public function __construct()
{
// Setup your plugin object here
}
public function enqueue_script()
{
// ...
}
}
Plugin::register();
這使WordPress代碼更加解耦。唯一的問題是,一旦對象注冊,就不能更改它。
構造函數現在也是公共的,沒有必要私有了。這意味著實例化一個新對象對WordPress不再有任何影響。
這些變化還有另一個積極的好處。可以進行類單元可測試。
將插件API拆分為自己的類,這是克服使用單例模式的更高級的方法,其思想是將插件API的使用與所有類分離。創(chuàng)建一個類,該類的唯一責任是與之交互。
這就是我在編寫使用接口的示例時想到的,創(chuàng)建該類就不需要在其他類中使用“add_action”和“add_filter”。我們在這里使用插件API,但是對于插件中非常依賴的任何API,都應該這樣做。
關于單例模式值得了解,也就是說,使用它是一種選擇,你不必這么做。對于你來說,有各種各樣的方法來回避單例模式解決的問題。