論WordPress的單例模式,和更好的初始化方法
WordPress中,插件使用單例模式,性能更好,免得資源浪費(fèi)。
平時(shí),以面向?qū)ο蟮膶懛ㄊ沁@樣的:
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();
或者,其他流行的方法是實(shí)例化一個(gè)對(duì)象類。在外部,使用該實(shí)例注冊(cè)你的動(dòng)作和過(guò)濾器。
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'));
這兩種方法都有一個(gè)問(wèn)題,那就是沒(méi)有一種簡(jiǎn)單的方法可以在之后將該對(duì)象取回,容易被覆蓋。當(dāng)然了,名字可以盡量起得不一樣。
實(shí)際上,WordPress同樣可以使用單例模式,實(shí)現(xiàn)單例模式的類有三個(gè)元素。你必須:
- 有一個(gè)私有靜態(tài)變量來(lái)存儲(chǔ)實(shí)例化的對(duì)象。
- 將構(gòu)造函數(shù)設(shè)為私有。
- 創(chuàng)建一個(gè)公共靜態(tài)方法(通常稱為“get_instance”)來(lái)獲取實(shí)例化的對(duì)象。
看一下前面的兩個(gè)例子。可以按照上述步驟將它們轉(zhuǎn)換為單例。生成的代碼與原始代碼非常接近。第一個(gè)示例使用構(gòu)造函數(shù)注冊(cè)所有操作和過(guò)濾器,可以在下面看到轉(zhuǎn)換后的代碼。
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();
將其轉(zhuǎn)換為單例需要進(jìn)行一些更改。將構(gòu)造函數(shù)設(shè)為私有,并添加了靜態(tài)變量和“get_instance”方法,結(jié)果與前面的示例幾乎相同。
同樣,可以放到外面來(lái)調(diào)用:
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'));
我們需要?jiǎng)?chuàng)建一個(gè)空的私有構(gòu)造函數(shù)。這是因?yàn)槟J(rèn)情況下構(gòu)造函數(shù)是公共的。私有構(gòu)造函數(shù)的另一個(gè)副作用是我們不能使用new關(guān)鍵字創(chuàng)建對(duì)象。我們需要通過(guò)調(diào)用“get_instance”將其傳遞給寄存器函數(shù)。值得注意的是,單例使第二種方法的吸引力大大降低。如果在構(gòu)造函數(shù)中注冊(cè)鉤子,那么單例模式更有趣。
從構(gòu)造函數(shù)中初始化插件
構(gòu)造函數(shù)的作用是準(zhǔn)備對(duì)象以供使用,用插件API注冊(cè)操作和過(guò)濾器根本不適合這個(gè)任務(wù)。使用靜態(tài)方法創(chuàng)建對(duì)象并在那里注冊(cè)所有內(nèi)容顯然更好。
class Plugin
{
/**
* 注冊(cè)插件
*/
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代碼更加解耦。唯一的問(wèn)題是,一旦對(duì)象注冊(cè),就不能更改它。
構(gòu)造函數(shù)現(xiàn)在也是公共的,沒(méi)有必要私有了。這意味著實(shí)例化一個(gè)新對(duì)象對(duì)WordPress不再有任何影響。
這些變化還有另一個(gè)積極的好處。可以進(jìn)行類單元可測(cè)試。
將插件API拆分為自己的類,這是克服使用單例模式的更高級(jí)的方法,其思想是將插件API的使用與所有類分離。創(chuàng)建一個(gè)類,該類的唯一責(zé)任是與之交互。
這就是我在編寫使用接口的示例時(shí)想到的,創(chuàng)建該類就不需要在其他類中使用“add_action”和“add_filter”。我們?cè)谶@里使用插件API,但是對(duì)于插件中非常依賴的任何API,都應(yīng)該這樣做。
關(guān)于單例模式值得了解,也就是說(shuō),使用它是一種選擇,你不必這么做。對(duì)于你來(lái)說(shuō),有各種各樣的方法來(lái)回避單例模式解決的問(wèn)題。