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

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

相关文章

ECShop设置模板的原理以及一些模板设置函数说明(个人观点)

ECShop设置模板的原理以及一些模板设置函数说明(个人观点) admin/includes/lib_template.php 一些函数及变量说明 $template_files...

PHP如何设置页面最大执行过期时间

一般在上传较大的附件时,超过30秒会出现网页过期的错误页面,如何处理这个问题呢?以下是处理方法: 1、如果是自己的服务器,可以全局配置:在php.ini 中设置,默认为30秒 max_execut...

新的月份,换一个新的模板

新的月份,换一个新的模板

用了半天的时间做一个emlog模板,自己感觉还是不错的。emlog模板做起来还是很简单的!...

可以让PHP编程事半功倍的类库

可以让PHP编程事半功倍的类库

在用php开发网站的时候,使用面向对象的方法确实可以提高代码复用率,减少代码冗余。而对初学者更友好的是,PHP开发网站所需要的大部分类库,网上都有十分优秀的类库存在了。作为一个程序猿当然不能重复制造轮...

PHP无限级分类的简单实现方法

PHP无限级分类的简单实现方法

PHP无限级分类的简单实现方法,请看截图:   代码是:  <?php  header("Content-type: text/html;&n...

PHP Apache2.2 虚拟主机配置

APACHE+PHP+MYSQL 基本环境设好了之后,今天在此基础之上设置 多个虚拟主机,即一个IP,可以有多个域名访问,也可以说一个WEB SERVER 上架设多个网站(在网上找了很久,都没有找...

发表评论

访客

看不清,换一张

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