-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathglobals.xml
143 lines (126 loc) · 4.16 KB
/
globals.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: d58ee8eaaa7f716c51f66f5f1058ab3c42376d98 Maintainer: takagi Status: ready -->
<refentry role="variable" xml:id="reserved.variables.globals" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>$GLOBALS</refname>
<refpurpose>グローバルスコープで使用可能なすべての変数への参照</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>
スクリプトのグローバルスコープに現在定義されているすべての変数への参照を含む連想配列です。
変数名が配列のキーとなります。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example xml:id="variable.globals.basic">
<title><varname>$GLOBALS</varname> の例</title>
<programlisting role="php">
<![CDATA[
<?php
function test()
{
$foo = "local variable";
echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
echo '$foo in current scope: ' . $foo . "\n";
}
$foo = "Example content";
test();
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
$foo in global scope: Example content
$foo in current scope: local variable
]]>
</screen>
</example>
</para>
<warning>
<para>
PHP 8.1.0 以降、<varname>$GLOBALS</varname> 配列全体を書き換える操作はサポートされなくなりました:
<example xml:id="variable.globals.entire_write_error">
<title><varname>$GLOBALS</varname> 配列全体を書き換える操作はエラーになる</title>
<programlisting role="php">
<![CDATA[
<?php
// 以下は、コンパイル時にエラーが発生します:
$GLOBALS = [];
$GLOBALS += [];
$GLOBALS =& $x;
$x =& $GLOBALS;
unset($GLOBALS);
array_pop($GLOBALS);
// ...上記以外の、$GLOBALS 全体に対するあらゆる書き込み/読み書き操作も同様です
?>
]]>
</programlisting>
</example>
</para>
</warning>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
¬e.is-superglobal;
<note>
<title>変数の可用性</title>
<para>
他の<link linkend="language.variables.superglobals">スーパーグローバル</link>
とは異なり、<varname>$GLOBALS</varname> は PHP で常に使用可能です。
</para>
</note>
<note>
<para>
PHP 8.1.0 以降、<varname>$GLOBALS</varname> はグローバルな <link linkend="features.gc.refcounting-basics">シンボルテーブル</link> の、読み取り専用のコピーになりました。つまり、グローバル変数は <varname>$GLOBALS</varname> のコピーを通じて書き換えられなくなったということです。
それより前のバージョンでは、
<varname>$GLOBALS</varname> 配列は値渡しの振る舞いの例外とされ、そのコピーを通じてグローバル変数の書き換えができていました。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// PHP 8.1.0 より前の振る舞い
$a = 1;
$globals = $GLOBALS; // 建前上は値渡しのコピー
$globals['a'] = 2;
var_dump($a); // int(2)
// PHP 8.1.0 以降の振る舞い
// 以下のようにしても、$a の値は変更されません。以前の振る舞いは、値渡しのセマンティクスに違反していました。
$globals = $GLOBALS;
$globals['a'] = 1;
// 以前の振る舞いを復元するには、$GLOBALS 配列のコピーを走査し、個別のキーと値を $GLOBALS 配列に書き戻すようにして下さい。
foreach ($globals as $key => $value) {
$GLOBALS[$key] = $value;
}
?>
]]>
</programlisting>
</informalexample>
</para>
</note>
</refsect1>
</refentry>
<!-- 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
-->