suchen
Heimphp教程php手册PHP5+引进$GLOBALS延迟初始化的概念

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

Jun 13, 2016 am 10:54 AM
server代码使用初始化延迟整理概念von

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

 

用例1:

 

 

print_r($GLOBALS);

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

 

Array

(

    [GLOBALS] => Array

 *RECURSION*

    [_POST] => Array

        (

        )

 

    [_GET] => Array

        (

        )

 

    [_COOKIE] => Array

        (

        )

 

    [_FILES] => Array

        (

        )

)

用例2:

 

 

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

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
4 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

VSCode Windows 64-Bit-Download

VSCode Windows 64-Bit-Download

Ein kostenloser und leistungsstarker IDE-Editor von Microsoft

mPDF

mPDF

mPDF ist eine PHP-Bibliothek, die PDF-Dateien aus UTF-8-codiertem HTML generieren kann. Der ursprüngliche Autor, Ian Back, hat mPDF geschrieben, um PDF-Dateien „on the fly“ von seiner Website auszugeben und verschiedene Sprachen zu verarbeiten. Es ist langsamer und erzeugt bei der Verwendung von Unicode-Schriftarten größere Dateien als Originalskripte wie HTML2FPDF, unterstützt aber CSS-Stile usw. und verfügt über viele Verbesserungen. Unterstützt fast alle Sprachen, einschließlich RTL (Arabisch und Hebräisch) und CJK (Chinesisch, Japanisch und Koreanisch). Unterstützt verschachtelte Elemente auf Blockebene (wie P, DIV),

MantisBT

MantisBT

Mantis ist ein einfach zu implementierendes webbasiertes Tool zur Fehlerverfolgung, das die Fehlerverfolgung von Produkten unterstützen soll. Es erfordert PHP, MySQL und einen Webserver. Schauen Sie sich unsere Demo- und Hosting-Services an.

WebStorm-Mac-Version

WebStorm-Mac-Version

Nützliche JavaScript-Entwicklungstools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung