|
簡介:
Allow definition of user defined constants in simple ini files, which are then processed like internal constants, without any
of the usual performance penalties.
允許使用簡單的ini文件來定義需要的常量,就像使用內(nèi)部變量一樣,而且沒有使用Define的性能問題。
作者說Hidef is initialized in php module init, before apache starts spawning children.
在apache啟動前,php啟動時創(chuàng)建并初始化了這些常量,這樣就不需要在php里define常量了,性能自然沒有任何問題了!
在Nginx下同樣可用,以下是安裝過程:
1、下載并解壓進入目錄
# wget http://pecl.php.NET/get/hidef-0.1.8.tgz
# tar zxvf hidef-0.1.8.tgz
# cd hidef-0.1.8
2、沒有configure文件,執(zhí)行phpize創(chuàng)建該文件
# /usr/local/webserver/php/bin/phpize
# ./configure --enable-hidef --with-php-config=/usr/local/webserver/php/bin/php-config
# make
# make install
3、添加到php.ini文件里面
# vi /usr/local/webserver/php/etc/php.ini
-----------------------------------------------
extension=hidef.so
hidef.ini_path=/usr/local/webserver/php/etc/
------------------------------------------------------------------------------
注意,如果php.ini文件里面沒有定義hidef.ini_path,則默認.ini文件讀取位置為/hidef,只需手工創(chuàng)建文件 vi /hidef/hidef.ini即可。
# vi /usr/local/webserver/php/etc/hidef.ini(此處根據(jù)情況自己調(diào)整路徑)
復制代碼 代碼如下:
[hidef]
int ANSWER = 42;
str HX = "9enjoy";
float PIE = 3.14159;
這里整數(shù)用int,浮點數(shù)用float,字符串用str。
字符串str的值使用雙引號來包含,或者直接寫字符串內(nèi)容。如果使用單引號,將會把單引號也做為字符串的內(nèi)容。
如str HX='9enjoy',實際存儲的不是9enjoy,是'9enjoy'。
4、重新加載php-fpm即可
# /usr/local/webserver/php/sbin/php-fpm reload
此時,查看phpinfo()的結果,在hidef處就可以看到定義的變量。

-----------------------------------------------------------------------------
附:
如果使用了APC,apc提供了定義常量的方法。apc_define_constants和apc_load_constants。apc_define_constants將常量轉(zhuǎn)為數(shù)組存到一個user cache中。雖然把常量存在了內(nèi)存中,但每次php請求時,仍然需要讀cache,分別定義,因此也不會有什么明顯的性能提升。我測試了下定義25個常量,使用apc的函數(shù)比直接定義常量快了0.01ms。
這樣使用:
if(!apc_load_constants('defined')) {
$constants = array(
'HX' => TRUE,
'D_BUG' => 1
);
apc_define_constants('defined', $constants);
}
define() is notoriously slow. Since the main benefit of APC is to increase the performance of scripts/applications, this mechanism is provided to streamline the process of mass constant definition. However, this function does not perform as well as anticipated.
For a better-performing solution, try the hidef extension from PECL.
APC的文檔中推薦使用hidef。
php技術:提高define性能的php擴展hidef的安裝和使用,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。