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

飘逸的风13年前 (2013-01-26)程序5753
今天在使用$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

相关文章

php正则取得网页上所有的link链接

$con=  file_get_contents($url); $pattern = '/<a(?:.*?)href="(((?:http(?:s?):\/\/)?([^\"\/]+...

文章内链的实现方法

在很多的时候,我在浏览其他的网站发现文章中一写关键词都被加上了链接链向对应的页面,我就在想他们是怎么实现这个功能的?为什么这么做,这么做有什么好处?   当然有什么好处一看便知,无论是从...

php日历的高效写法

标题写的吸引人了些,不过我就觉得是挺高效的,我相信 之前看到过 irlvirus 写的 《php练习代码-日历》,我觉得效率都不是很好。 date()函数结合mktime() 可以得到一切东西...

emlog SyntaxHighlighter 代码高亮插件[提供下载,更新至1.2]

emlog SyntaxHighlighter 代码高亮插件[提供下载,更新至1.2]

emlog SyntaxHighlighter 代码高亮插件,可以把代码贴在代码框中,确定后自动生成高亮代码。 注:本插件已升级,修复在代码很长换行的时候左边的行数不增加问题;插入代码里有HTML标...

ECShop仿淘宝商品详细页 实现尺码颜色关联显示库存

ECShop仿淘宝商品详细页 实现尺码颜色关联显示库存

  ecshop服装商城很多都十分需要一个颜色尺码的功能,其实在淘宝,凡客上都类似的功能,客户在下单时选容易选择相应的颜色尺码,其实ecshop要实现这样的功能并不难,因为ecshop是开源免费的。...

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

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

发表评论

访客

看不清,换一张

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