Home Backend Development PHP Tutorial PHP5+引进$GLOBALS推迟初始化的概念

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

Jun 13, 2016 pm 12:55 PM
array nbsp php server

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

  作者:zhanhailiang 日期:2013-01-18
Copy after login

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

用例1:

<span class="kw2" style="color:rgb(0,0,0); font-weight:bold"><?php</span>
 
<span class="kw3" style="color:rgb(0,0,102)">print_r</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$GLOBALS</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span>
Copy after login

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

<span class="kw3" style="color:rgb(0,0,102)">Array</span>
<span class="br0" style="color:rgb(102,204,102)">(</span>
    <span class="br0" style="color:rgb(102,204,102)">[</span>GLOBALS<span class="br0" style="color:rgb(102,204,102)">]</span> <span class="sy0" style="color:rgb(102,204,102)">=></span> <span class="kw3" style="color:rgb(0,0,102)">Array</span>
 <span class="sy0" style="color:rgb(102,204,102)">*</span>RECURSION<span class="sy0" style="color:rgb(102,204,102)">*</span>
    <span class="br0" style="color:rgb(102,204,102)">[</span>_POST<span class="br0" style="color:rgb(102,204,102)">]</span> <span class="sy0" style="color:rgb(102,204,102)">=></span> <span class="kw3" style="color:rgb(0,0,102)">Array</span>
        <span class="br0" style="color:rgb(102,204,102)">(</span>
        <span class="br0" style="color:rgb(102,204,102)">)</span>
 
    <span class="br0" style="color:rgb(102,204,102)">[</span>_GET<span class="br0" style="color:rgb(102,204,102)">]</span> <span class="sy0" style="color:rgb(102,204,102)">=></span> <span class="kw3" style="color:rgb(0,0,102)">Array</span>
        <span class="br0" style="color:rgb(102,204,102)">(</span>
        <span class="br0" style="color:rgb(102,204,102)">)</span>
 
    <span class="br0" style="color:rgb(102,204,102)">[</span>_COOKIE<span class="br0" style="color:rgb(102,204,102)">]</span> <span class="sy0" style="color:rgb(102,204,102)">=></span> <span class="kw3" style="color:rgb(0,0,102)">Array</span>
        <span class="br0" style="color:rgb(102,204,102)">(</span>
        <span class="br0" style="color:rgb(102,204,102)">)</span>
 
    <span class="br0" style="color:rgb(102,204,102)">[</span>_FILES<span class="br0" style="color:rgb(102,204,102)">]</span> <span class="sy0" style="color:rgb(102,204,102)">=></span> <span class="kw3" style="color:rgb(0,0,102)">Array</span>
        <span class="br0" style="color:rgb(102,204,102)">(</span>
        <span class="br0" style="color:rgb(102,204,102)">)</span>
<span class="br0" style="color:rgb(102,204,102)">)</span>
Copy after login

用例2:

<span class="kw2" style="color:rgb(0,0,0); font-weight:bold"><?php</span><span style="color:#333333">
 
</span><span class="kw3" style="color:rgb(0,0,102)">print_r</span><span class="br0" style="color:rgb(102,204,102)">(</span><span class="re0" style="color:rgb(0,0,255)">$GLOBALS</span><span class="br0" style="color:rgb(102,204,102)">)</span><span class="sy0" style="color:rgb(102,204,102)">;</span><span style="color:#333333">
</span><span style="color:#ff0000"><span class="kw3">print_r</span><span class="br0">(</span><span class="re0">$_SERVER</span><span class="br0">)</span><span class="sy0">;</span></span>
Copy after login

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

<span class="kw3" style="color:rgb(0,0,102)">Array</span><span style="color:#333333">
</span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">
    </span><span class="br0" style="color:rgb(102,204,102)">[</span><span style="color:#333333">GLOBALS</span><span class="br0" style="color:rgb(102,204,102)">]</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=></span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">Array</span><span style="color:#333333">
 </span><span class="sy0" style="color:rgb(102,204,102)">*</span><span style="color:#333333">RECURSION</span><span class="sy0" style="color:rgb(102,204,102)">*</span><span style="color:#333333">
    </span><span class="br0" style="color:rgb(102,204,102)">[</span><span style="color:#333333">_POST</span><span class="br0" style="color:rgb(102,204,102)">]</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=></span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">Array</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">
 
    </span><span class="br0" style="color:rgb(102,204,102)">[</span><span style="color:#333333">_GET</span><span class="br0" style="color:rgb(102,204,102)">]</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=></span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">Array</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">
 
    </span><span class="br0" style="color:rgb(102,204,102)">[</span><span style="color:#333333">_COOKIE</span><span class="br0" style="color:rgb(102,204,102)">]</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=></span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">Array</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">
 
    </span><span class="br0" style="color:rgb(102,204,102)">[</span><span style="color:#333333">_FILES</span><span class="br0" style="color:rgb(102,204,102)">]</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=></span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">Array</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">
 
    </span><span class="br0" style="color:rgb(102,204,102)">[</span><span style="color:#ff0000">_SERVER</span><span class="br0" style="color:rgb(102,204,102)">]</span><span style="color:#333333"> </span><span class="sy0" style="color:rgb(102,204,102)">=></span><span style="color:#333333"> </span><span class="kw3" style="color:rgb(0,0,102)">Array</span><span style="color:#333333">
        </span><span class="br0" style="color:rgb(102,204,102)">(</span><span style="color:#333333">
 
        </span><span class="br0" style="color:rgb(102,204,102)">)</span><span style="color:#333333">
 
</span><span class="br0" style="color:rgb(102,204,102)">)</span>
Copy after login

查了下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!)

<span style="color:#333333"> </span><span class="nu0" style="color:rgb(204,102,204)">718</span><span style="color:#333333"> </span><span class="co0" style="color:rgb(128,128,128); font-style:italic">; </span><span class="co0" style="font-style:italic"><span style="color:#ff0000">When enabled, the SERVER and ENV variables are created when they're first</span></span><span style="color:#333333">
 </span><span class="nu0" style="color:rgb(204,102,204)">719</span><span style="color:#333333"> </span><span class="co0" style="color:rgb(128,128,128); font-style:italic">; </span><span class="co0" style="font-style:italic"><span style="color:#ff0000">used (Just In Time) instead of when the script starts.</span></span><span class="co0" style="color:rgb(128,128,128); font-style:italic"> If these variables</span><span style="color:#333333">
 </span><span class="nu0" style="color:rgb(204,102,204)">720</span><span style="color:#333333"> </span><span class="co0" style="color:rgb(128,128,128); font-style:italic">; are not used within a script, having this directive on will result in a</span><span style="color:#333333">
 </span><span class="nu0" style="color:rgb(204,102,204)">721</span><span style="color:#333333"> </span><span class="co0" style="color:rgb(128,128,128); font-style:italic">; performance gain. The PHP directives register_globals, register_long_arrays,</span><span style="color:#333333">
 </span><span class="nu0" style="color:rgb(204,102,204)">722</span><span style="color:#333333"> </span><span class="co0" style="color:rgb(128,128,128); font-style:italic">; and register_argc_argv must be disabled for this directive to have any affect.</span><span style="color:#333333">
 </span><span class="nu0" style="color:rgb(204,102,204)">723</span><span style="color:#333333"> </span><span class="co0" style="color:rgb(128,128,128); font-style:italic">; http://php.net/auto-globals-jit</span><span style="color:#333333">
 </span><span class="nu0" style="color:rgb(204,102,204)">724</span><span style="color:#333333"> auto_globals_jit </span><span class="sy0" style="color:rgb(102,204,102)">=</span><span class="re2" style="color:rgb(0,0,255)"> On</span>
Copy after login

终于弄明白了,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也只会在第一次使用时才会创建。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles