Skip to content

Commit

Permalink
Sync array functions (php#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored Sep 9, 2024
1 parent e576e95 commit cd5aec4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 57 deletions.
39 changes: 22 additions & 17 deletions language/predefined/variables/globals.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: a6d209f4ff71ccba3f1255902827f5df3e092ff9 Maintainer: daijie Status: ready -->

<!-- EN-Revision: d58ee8eaaa7f716c51f66f5f1058ab3c42376d98 Maintainer: daijie Status: ready -->
<!-- CREDITS: Luffy -->
<refentry role="variable" xml:id="reserved.variables.globals" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" annotations="verify_info:false">
<refnamediv>
<refname>$GLOBALS</refname>
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,18 @@ $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 @@ -80,15 +83,17 @@ $foo in current scope: local variable
</note>
<note>
<para>
从 PHP 8.1.0 起, <varname>$GLOBALS</varname> 现在是全局符号表的只读副本
从 PHP 8.1.0 起,<varname>$GLOBALS</varname> 现在是全局<link linkend="features.gc.refcounting-basics">符号表</link>的只读副本
也就是说,全局变量不能通过副本进行修改。
在之前的版本中,<varname>$GLOBALS</varname> 数组和 PHP 数组通常传值的行为不一样,全局变量可通过副本修改。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
// PHP 8.1.0 之前
$a = 1;
$globals = $GLOBALS; // 表面意义的按值复制
$globals['a'] = 2;
var_dump($a); // int(2)
Expand All @@ -102,6 +107,7 @@ $globals['a'] = 1;
foreach ($globals as $key => $value) {
$GLOBALS[$key] = $value;
}
?>
]]>
</programlisting>
Expand Down Expand Up @@ -132,4 +138,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 cd5aec4

Please sign in to comment.