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

飘逸的风13年前 (2013-01-26)程序5699
今天在使用$GLOBALS['_SERVER']来替代$_SERVER来访问相关的环境变量,总是会报“_SERVER undefined”错误。如下用例:   

用例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 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 ; https://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也只会在第一次使用时才会创建。

FROM:https://www.php100.com/html/webkaifa/PHP/PHP/2013/0124/11987.html

相关文章

Emlog常用日志列表页的判断

  Emlog 用得久了,emer 们就难免会折腾一下自己的模板,谁让 Emlog 这么简单实用呢?除了折腾外观及各种特效,当然还有实用的功能,日志列表页的判断应该就属于此类。我们可以根据日志列表页面...

PHP给图片添加水印和生成缩略图函数

给图片添加水印: /************************************ 函数: watermark($bigimg, $smallimg, $coord = 1) 作用:...

PHP购物车类,简单易用,移植CodeIgniter,并进行一些优化[附带实例]

个人感觉CodeIgniter用起来方便,但有时候做电子商城网站的时候,没有使用CodeIgniter框架,但想用CodeIgniter里的购物车程序,那么就需要对其购物车类进行修改。但CodeIgn...

如何在 SAE 下操作本地IO

因为SAE平台安全性的考虑,限制了用户对于本地IO的使用,但这样对于一些传统的PHP项目,也许带来了很多不便,因为它们都或多或少的有对本地IO的操作,像Smarty的编译模板和文件上传程序。为了解决这...

10个你可能从未用过的PHP函数

1. sys_getloadavg() sys_getloadavt()可以获得系统负载情况。该函数返回一个包含三个元素的数组,每个元素分别代表系统再过去的1、5和15分钟内的平均负载。...

htaccess 防止盗链,防止目录浏览等10大技巧

1. 反盗链 那些盗用了你的内容,还不愿意自己存储图片的网站是无耻的。你可以通过以下配置来放置别人盗用你的图片:   1     RewriteBase...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。