首页 > php教程 > php手册 > 正文

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

php中文网
发布: 2016-06-13 10:54:19
原创
1056人浏览过

今天整理代码想使用$GLOBALS['_SERVER']来替代$_SERVER来访问相关的环境变量,总是会报“_SERVER undefined”错误。如下用例:

 

立即学习PHP免费学习笔记(深入)”;

AI Room Planner
AI Room Planner

AI 室内设计工具,免费为您的房间提供上百种设计方案

AI Room Planner 136
查看详情 AI Room Planner

用例1:

 

立即学习PHP免费学习笔记(深入)”;

 

立即学习PHP免费学习笔记(深入)”;

print_r($GLOBALS);

此时输出中并没有_SERVER相关信息:

 

立即学习PHP免费学习笔记(深入)”;

Array

(

    [GLOBALS] => Array

 *RECURSION*

    [_POST] => Array

        (

        )

 

立即学习PHP免费学习笔记(深入)”;

    [_GET] => Array

        (

        )

 

立即学习PHP免费学习笔记(深入)”;

    [_COOKIE] => Array

        (

        )

 

立即学习PHP免费学习笔记(深入)”;

    [_FILES] => Array

        (

        )

)

用例2:

 

立即学习PHP免费学习笔记(深入)”;

 

立即学习PHP免费学习笔记(深入)”;

print_r($GLOBALS);

print_r($_SERVER);

此时输出中含有_SERVER相关信息:

 

立即学习PHP免费学习笔记(深入)”;

Array

(

    [GLOBALS] => Array

 *RECURSION*

    [_POST] => Array

        (

        )

 

立即学习PHP免费学习笔记(深入)”;

    [_GET] => Array

        (

        )

 

立即学习PHP免费学习笔记(深入)”;

    [_COOKIE] => Array

        (

        )

 

立即学习PHP免费学习笔记(深入)”;

    [_FILES] => Array

        (

        )

 

立即学习PHP免费学习笔记(深入)”;

    [_SERVER] => Array

        (

 

立即学习PHP免费学习笔记(深入)”;

        )

 

立即学习PHP免费学习笔记(深入)”;

)

查了下PHP手册关于$GLOBALS描述,引用therandshow at gmail dot com的评论:

 

立即学习PHP免费学习笔记(深入)”;

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更新日志的描述:

 

立即学习PHP免费学习笔记(深入)”;

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时才会创建。所以就会出现上述两个用例的情况。

 

立即学习PHP免费学习笔记(深入)”;

备注:

 

立即学习PHP免费学习笔记(深入)”;

实测结论:

 

立即学习PHP免费学习笔记(深入)”;

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

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号