您好,欢迎访问一九零五行业门户网

PHP5+引进$GLOBALS延迟初始化概念_PHP教程

今天在使用$globals['_server']来替代$_server来访问相关的环境变量,总是会报“_server undefined”错误。如下用例:   
用例1:
    array
    *recursion*
    [_post] => array
    (
    )
    [_get] => array
    (
    )
    [_cookie] => array
    (
    )
    [_files] => array
    (
    )
    )
用例2:
    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 of php 5.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!)www.2cto.com
    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
    终于弄明白了,php5+中在开启auto_globals_jit = on情况下,$_server变量和$_env变量不会在脚本启动时就创建,而是会在第一次使用$server和$env时才会创建。所以就会出现上述两个用例的情况。
备注:
   实测结论:
    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也只会在第一次使用时才会创建。
http://www.bkjia.com/phpjc/735083.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/735083.htmltecharticle今天在使用$globals[_server]来替代$_server来访问相关的环境变量,总是会报_server undefined错误。如下用例: 用例1: ?php print_r($globals); 此时...
其它类似信息

推荐信息