PHP 5+導入$GLOBALS遅延初期化の概念
12427 ワード
:zhanhailiang :2013-01-18
今日整理コードは$GLOBALS['_SERVER']を使って$SERVERは関連する環境変数にアクセスし、常に「_SERVERundefined」エラーを報告します.次の例を示します.
使用例1:
<?php
print_r($GLOBALS);
このとき出力に_はありませんSERVER関連情報:
Array
(
[GLOBALS] => Array
*RECURSION*
[_POST] => Array
(
)
[_GET] => Array
(
)
[_COOKIE] => Array
(
)
[_FILES] => Array
(
)
)
使用例2:
<?php
print_r($GLOBALS);
print_r($_SERVER);
このとき出力には_が含まれますSERVER関連情報:
Array
(
[GLOBALS] => Array
*RECURSION*
[_POST] => Array
(
)
[_GET] => Array
(
)
[_COOKIE] => Array
(
)
[_FILES] => Array
(
)
[_SERVER] => Array
(
)
)
PHPマニュアル$GLOBALSの説明を調べ、therandshow at gmail dot comのコメントを引用します.
therandshow at gmail dot com
As ofPHP5.4 $GLOBALS is now initialized just-in-time.
This means there now is an advantage to not use
the $GLOBALS variable as you can avoid the overhead of initializing it. How much of an advantage that is
I'm not sure, but I've never liked $GLOBALS much anyways.
ルート数のソースを追跡すると、PHP5Changelog更新ログの説明が見つかりました.
Unordered List ItemImproved Zend Engine, performance tweaks and optimizations
Unordered List ItemChanged $GLOBALS into a JIT autoglobal, so it's initialized only if used. (this may affect opcode caches!)
718 ; When enabled, the SERVER and ENV variables are created when they're first
719 ; used (Just In Time) instead of when the script starts. If these variables
720 ; are not used within a script, having this directive on will result in a
721 ; performance gain. The PHP directives register_globals, register_long_arrays,
722 ; and register_argc_argv must be disabled for this directive to have any affect.
723 ; http://php.net/auto-globals-jit
724 auto_globals_jit = On
やっと分かった、PHP 5+でauto_をオンにglobals_jit=Onの場合、$SERVER変数と$ENV変数は、スクリプトの開始時には作成されませんが、$SERVERと$ENVが初めて使用されたときに作成されます.したがって、上記の2つの例が現れる.
コメント:
実測の結論:
auto_globals_jit setting is also affecting $_REQUEST superglobal in 5.3 It is not explicitly stated in documentation.
少なくとも5.3.13バージョンでauto_を開くglobals_jit=Onの場合、$REQUESTも初回使用時のみ作成されます.