PHP5+引进$GLOBALS推迟初始化的概念
PHP5+引进$GLOBALS延迟初始化的概念
作者:zhanhailiang 日期:2013-01-18
今天整理代码想使用$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>
此时输出中并没有_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>
用例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>
此时输出中含有_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>
查了下PHP手册关于$GLOBALS描述,引用therandshow at gmail dot com的评论:
therandshow at gmail dot comAs 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 optimizationsUnordered 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>Copier après la connexion终于弄明白了,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也只会在第一次使用时才会创建。Déclaration de ce site WebLe contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn![]()
Outils d'IA chauds
![]()
Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes
![]()
AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.
![]()
Undress AI Tool
Images de déshabillage gratuites
![]()
Clothoff.io
Dissolvant de vêtements AI
![]()
AI Hentai Generator
Générez AI Hentai gratuitement.
![]()
Article chaud
R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌R.E.P.O. Meilleurs paramètres graphiques3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌Assassin's Creed Shadows: Solution d'énigmes de coquille2 Il y a quelques semaines By DDDR.E.P.O. Comment réparer l'audio si vous n'entendez personne3 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌WWE 2K25: Comment déverrouiller tout dans Myrise4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌![]()
Outils chauds
![]()
Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit
![]()
SublimeText3 version chinoise
Version chinoise, très simple à utiliser
![]()
Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP
![]()
Dreamweaver CS6
Outils de développement Web visuel
![]()
SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)
See all articlesGuide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian Dec 24, 2024 pm 04:42 PM
PHP 8.4 apporte plusieurs nouvelles fonctionnalités, améliorations de sécurité et de performances avec une bonne quantité de dépréciations et de suppressions de fonctionnalités. Ce guide explique comment installer PHP 8.4 ou mettre à niveau vers PHP 8.4 sur Ubuntu, Debian ou leurs dérivés. Bien qu'il soit possible de compiler PHP à partir des sources, son installation à partir d'un référentiel APT comme expliqué ci-dessous est souvent plus rapide et plus sécurisée car ces référentiels fourniront les dernières corrections de bogues et mises à jour de sécurité à l'avenir.
Date et heure de CakePHP Sep 10, 2024 pm 05:27 PM
Pour travailler avec la date et l'heure dans cakephp4, nous allons utiliser la classe FrozenTime disponible.
Discuter de CakePHP Sep 10, 2024 pm 05:28 PM
CakePHP est un framework open source pour PHP. Il vise à faciliter grandement le développement, le déploiement et la maintenance d'applications. CakePHP est basé sur une architecture de type MVC à la fois puissante et facile à appréhender. Modèles, vues et contrôleurs gu
Téléchargement de fichiers CakePHP Sep 10, 2024 pm 05:27 PM
Pour travailler sur le téléchargement de fichiers, nous allons utiliser l'assistant de formulaire. Voici un exemple de téléchargement de fichiers.
CakePHP créant des validateurs Sep 10, 2024 pm 05:26 PM
Le validateur peut être créé en ajoutant les deux lignes suivantes dans le contrôleur.
Comment configurer Visual Studio Code (VS Code) pour le développement PHP Dec 20, 2024 am 11:31 AM
Visual Studio Code, également connu sous le nom de VS Code, est un éditeur de code source gratuit – ou environnement de développement intégré (IDE) – disponible pour tous les principaux systèmes d'exploitation. Avec une large collection d'extensions pour de nombreux langages de programmation, VS Code peut être c
Guide rapide CakePHP Sep 10, 2024 pm 05:27 PM
CakePHP est un framework MVC open source. Cela facilite grandement le développement, le déploiement et la maintenance des applications. CakePHP dispose d'un certain nombre de bibliothèques pour réduire la surcharge des tâches les plus courantes.
Comment analysez-vous et traitez-vous HTML / XML dans PHP? Feb 07, 2025 am 11:57 AM
Ce tutoriel montre comment traiter efficacement les documents XML à l'aide de PHP. XML (Language de balisage extensible) est un langage de balisage basé sur le texte polyvalent conçu à la fois pour la lisibilité humaine et l'analyse de la machine. Il est couramment utilisé pour le stockage de données et
![]()