Skip to content

Commit

Permalink
Add a link to the explanation of the term 'symbol table' + slightly a…
Browse files Browse the repository at this point in the history
…djust the code of the examples

php/doc-en@d58ee8e
  • Loading branch information
mumumu committed Sep 1, 2024
1 parent 7643745 commit bd4aec1
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 37 deletions.
39 changes: 23 additions & 16 deletions language/predefined/variables/globals.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: a7f535a32ba19f7e8236b9d87f3d84552c5f9b64 Maintainer: takagi Status: ready -->
<!-- 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>
Expand All @@ -24,7 +24,9 @@
<programlisting role="php">
<![CDATA[
<?php
function test() {
function test()
{
$foo = "local variable";
echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
Expand All @@ -33,6 +35,7 @@ function test() {
$foo = "Example content";
test();
?>
]]>
</programlisting>
Expand All @@ -51,18 +54,20 @@ $foo in current scope: local variable
<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 全体に対するあらゆる書き込み/読み書き操作も同様です
?>
]]>
<![CDATA[
<?php
// 以下は、コンパイル時にエラーが発生します:
$GLOBALS = [];
$GLOBALS += [];
$GLOBALS =& $x;
$x =& $GLOBALS;
unset($GLOBALS);
array_pop($GLOBALS);
// ...上記以外の、$GLOBALS 全体に対するあらゆる書き込み/読み書き操作も同様です
?>
]]>
</programlisting>
</example>
</para>
Expand All @@ -81,15 +86,17 @@ $foo in current scope: local variable
</note>
<note>
<para>
PHP 8.1.0 以降、<varname>$GLOBALS</varname> はグローバルなシンボルテーブルの、読み取り専用のコピーになりました。つまり、グローバル変数は <varname>$GLOBALS</varname> のコピーを通じて書き換えられなくなったということです。
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)
Expand All @@ -103,6 +110,7 @@ $globals['a'] = 1;
foreach ($globals as $key => $value) {
$GLOBALS[$key] = $value;
}
?>
]]>
</programlisting>
Expand Down Expand Up @@ -133,4 +141,3 @@ vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->

Loading

0 comments on commit bd4aec1

Please sign in to comment.