Files
doc-fr/language/predefined/variables/globals.xml
2021-12-29 15:32:58 +00:00

142 lines
3.9 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: a7f535a32ba19f7e8236b9d87f3d84552c5f9b64 Maintainer: yannick Status: ready -->
<!-- Reviewed: yes Maintainer: mikaelkael -->
<phpdoc:varentry xmlns:phpdoc="http://php.net/ns/phpdoc" xml:id="reserved.variables.globals" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>$GLOBALS</refname>
<refpurpose>Référence toutes les variables disponibles dans un contexte global</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>
Un tableau associatif contenant les références sur toutes les variables
globales actuellement définies dans le contexte d'exécution global du
script. Les noms des variables sont les index du tableau.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example xml:id="variable.globals.basic">
<title>Exemple avec <varname>$GLOBALS</varname></title>
<programlisting role="php">
<![CDATA[
<?php
function test() {
$foo = "variable locale";
echo '$foo dans le contexte global : ' . $GLOBALS["foo"] . "\n";
echo '$foo dans le contexte courant : ' . $foo . "\n";
}
$foo = "Exemple de contenu";
test();
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
$foo dans le contexte global : Exemple de contenu
$foo dans le contexte courant : variable locale
]]>
</screen>
</example>
</para>
<warning>
<para>
À partir de PHP 8.1.0, l'accès en écriture au tableau entier
<varname>$GLOBALS</varname> n'est désormais plus supportée :
<example xml:id="variable.globals.entire_write_error">
<title>Écrire au tableau entier <varname>$GLOBALS</varname> résultera en une erreur.</title>
<programlisting role="php">
<![CDATA[
<?php
// Génère une erreur lors de la compilation :
$GLOBALS = [];
$GLOBALS += [];
$GLOBALS =& $x;
$x =& $GLOBALS;
unset($GLOBALS);
array_pop($GLOBALS);
// ...et toute autre opération d'écriture/lecture-écriture sur $GLOBALS
?>
]]>
</programlisting>
</example>
</para>
</warning>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
&note.is-superglobal;
<note>
<title>Disponibilité des variables</title>
<para>
Contrairement à toutes les autres
<link linkend="language.variables.superglobals">superglobales</link>,
<varname>$GLOBALS</varname> a toujours été disponible en PHP.
</para>
</note>
<note>
<para>
À partir de PHP 8.1.0, <varname>$GLOBALS</varname> est désormais une
copie en lecture seule du tableau de symbole global.
C'est-à-dire que, les variables globales ne peuvent pas être modifié via sa copie.
Auparavant, le tableau <varname>$GLOBALS</varname> était exclus du
comportement usuel par-valeur des tableaux PHP et les variables globales
peuvent être modifié via sa copie.
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// Before PHP 8.1.0
$a = 1;
$globals = $GLOBALS; // Ostensibly by-value copy
$globals['a'] = 2;
var_dump($a); // int(2)
// As of PHP 8.1.0
// this no longer modifies $a. The previous behavior violated by-value semantics.
$globals = $GLOBALS;
$globals['a'] = 1;
// To restore the previous behavior, iterate its copy and assign each property back to $GLOBALS.
foreach ($globals as $key => $value) {
$GLOBALS[$key] = $value;
}
?>
]]>
</programlisting>
</informalexample>
</para>
</note>
</refsect1>
</phpdoc:varentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->