language/predefined/variables/globals.xml
a6d209f4ff71ccba3f1255902827f5df3e092ff9
...
...
@@ -1,7 +1,7 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
3

4
-
<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">
4
+
<refentry role="variable" xml:id="reserved.variables.globals" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
5
5
<refnamediv>
6
6
<refname>$GLOBALS</refname>
7
7
<refpurpose>References all variables available in global scope</refpurpose>
...
...
@@ -45,6 +45,28 @@ $foo in current scope: local variable
45
45
</screen>
46
46
</example>
47
47
</para>
48
+
<warning>
49
+
<para>
50
+
As of PHP 8.1.0, write access to the entire <varname>$GLOBALS</varname> array is no longer supported:
51
+
<example xml:id="variable.globals.entire_write_error">
52
+
<title>writing entire <varname>$GLOBALS</varname> will result in error.</title>
53
+
<programlisting role="php">
54
+
<![CDATA[
55
+
<?php
56
+
// Generates compile-time error:
57
+
$GLOBALS = [];
58
+
$GLOBALS += [];
59
+
$GLOBALS =& $x;
60
+
$x =& $GLOBALS;
61
+
unset($GLOBALS);
62
+
array_pop($GLOBALS);
63
+
// ...and any other write/read-write operation on $GLOBALS
64
+
?>
65
+
]]>
66
+
</programlisting>
67
+
</example>
68
+
</para>
69
+
</warning>
48
70
</refsect1>
49
71
50
72
<refsect1 role="notes">
...
...
@@ -57,9 +79,37 @@ $foo in current scope: local variable
57
79
<varname>$GLOBALS</varname> has essentially always been available in PHP.
58
80
</para>
59
81
</note>
82
+
<note>
83
+
<para>
84
+
As of PHP 8.1.0, <varname>$GLOBALS</varname> is now a read-only copy of the global symbol table. That is, global variables cannot be modified via its copy. Previously, <varname>$GLOBALS</varname> array is excluded from the usual by-value behavior of PHP arrays and global variables can be modified via its copy.
85
+
<informalexample>
86
+
<programlisting role="php">
87
+
<![CDATA[
88
+
<?php
89
+
// Before PHP 8.1.0
90
+
$a = 1;
91
+
$globals = $GLOBALS; // Ostensibly by-value copy
92
+
$globals['a'] = 2;
93
+
var_dump($a); // int(2)
94
+

95
+
// As of PHP 8.1.0
96
+
// this no longer modifies $a. The previous behavior violated by-value semantics.
97
+
$globals = $GLOBALS;
98
+
$globals['a'] = 1;
99
+

100
+
// To restore the previous behavior, iterate its copy and assign each property back to $GLOBALS.
101
+
foreach ($globals as $key => $value) {
102
+
$GLOBALS[$key] = $value;
103
+
}
104
+
?>
105
+
]]>
106
+
</programlisting>
107
+
</informalexample>
108
+
</para>
109
+
</note>
60
110
</refsect1>
61
111
62
-
</phpdoc:varentry>
112
+
</refentry>
63
113
64
114
<!-- Keep this comment at the end of the file
65
115
Local variables:
66
116