language/predefined/variables/globals.xml
d58ee8eaaa7f716c51f66f5f1058ab3c42376d98
d58ee8eaaa7f716c51f66f5f1058ab3c42376d98
...
...
@@ -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>
...
...
@@ -24,7 +24,9 @@
24
24
<programlisting role="php">
25
25
<![CDATA[
26
26
<?php
27
-
function test() {
27
+
28
+
function test()
29
+
{
28
30
$foo = "local variable";
29
31
30
32
echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
...
...
@@ -33,6 +35,7 @@ function test() {
33
35
34
36
$foo = "Example content";
35
37
test();
38
+
36
39
?>
37
40
]]>
38
41
</programlisting>
...
...
@@ -45,6 +48,30 @@ $foo in current scope: local variable
45
48
</screen>
46
49
</example>
47
50
</para>
51
+
<warning>
52
+
<para>
53
+
As of PHP 8.1.0, write access to the entire <varname>$GLOBALS</varname> array is no longer supported:
54
+
<example xml:id="variable.globals.entire_write_error">
55
+
<title>writing entire <varname>$GLOBALS</varname> will result in error.</title>
56
+
<programlisting role="php">
57
+
<![CDATA[
58
+
<?php
59
+
60
+
// Generates compile-time error:
61
+
$GLOBALS = [];
62
+
$GLOBALS += [];
63
+
$GLOBALS =& $x;
64
+
$x =& $GLOBALS;
65
+
unset($GLOBALS);
66
+
array_pop($GLOBALS);
67
+
// ...and any other write/read-write operation on $GLOBALS
68
+
69
+
?>
70
+
]]>
71
+
</programlisting>
72
+
</example>
73
+
</para>
74
+
</warning>
48
75
</refsect1>
49
76
50
77
<refsect1 role="notes">
...
...
@@ -57,9 +84,44 @@ $foo in current scope: local variable
57
84
<varname>$GLOBALS</varname> has essentially always been available in PHP.
58
85
</para>
59
86
</note>
87
+
<note>
88
+
<para>
89
+
As of PHP 8.1.0, <varname>$GLOBALS</varname> is now a read-only copy of
90
+
the global <link linkend="features.gc.refcounting-basics">symbol table</link>.
91
+
That is, global variables cannot be modified via its copy.
92
+
Previously, <varname>$GLOBALS</varname> array is excluded from the usual
93
+
by-value behavior of PHP arrays and global variables can be modified via its copy.
94
+
<informalexample>
95
+
<programlisting role="php">
96
+
<![CDATA[
97
+
<?php
98
+
99
+
// Before PHP 8.1.0
100
+
$a = 1;
101
+
102
+
$globals = $GLOBALS; // Ostensibly by-value copy
103
+
$globals['a'] = 2;
104
+
var_dump($a); // int(2)
105
+
106
+
// As of PHP 8.1.0
107
+
// this no longer modifies $a. The previous behavior violated by-value semantics.
108
+
$globals = $GLOBALS;
109
+
$globals['a'] = 1;
110
+
111
+
// To restore the previous behavior, iterate its copy and assign each property back to $GLOBALS.
112
+
foreach ($globals as $key => $value) {
113
+
$GLOBALS[$key] = $value;
114
+
}
115
+
116
+
?>
117
+
]]>
118
+
</programlisting>
119
+
</informalexample>
120
+
</para>
121
+
</note>
60
122
</refsect1>
61
123
62
-
</phpdoc:varentry>
124
+
</refentry>
63
125
64
126
<!-- Keep this comment at the end of the file
65
127
Local variables:
...
...
@@ -81,4 +143,3 @@ vim600: syn=xml fen fdm=syntax fdl=2 si
81
143
vim: et tw=78 syn=sgml
82
144
vi: ts=1 sw=1
83
145
-->
84
-
85
146