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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect2 xml:id="migration70.incompatible.variable-handling">
<title>Changes to variable handling</title>
<para>
PHP 7 now uses an abstract syntax tree when parsing source files. This has
permitted many improvements to the language which were previously
impossible due to limitations in the parser used in earlier versions of
PHP, but has resulted in the removal of a few special cases for consistency
reasons, which has resulted in backward compatibility breaks. These cases
are detailed in this section.
</para>
<sect3 xml:id="migration70.incompatible.variable-handling.indirect">
<title>
Changes to the handling of indirect variables, properties, and methods
</title>
<para>
Indirect access to variables, properties, and methods will now be
evaluated strictly in left-to-right order, as opposed to the previous mix
of special cases. The table below shows how the order of evaluation has
changed.
</para>
<para>
<table>
<title>Old and new evaluation of indirect expressions</title>
<tgroup cols="3">
<thead>
<row>
<entry>Expression</entry>
<entry>PHP 5 interpretation</entry>
<entry>PHP 7 interpretation</entry>
</row>
</thead>
<tbody>
<row>
<entry>
<code>$$foo['bar']['baz']</code>
</entry>
<entry>
<code>${$foo['bar']['baz']}</code>
</entry>
<entry>
<code>($$foo)['bar']['baz']</code>
</entry>
</row>
<row>
<entry>
<code>$foo->$bar['baz']</code>
</entry>
<entry>
<code>$foo->{$bar['baz']}</code>
</entry>
<entry>
<code>($foo->$bar)['baz']</code>
</entry>
</row>
<row>
<entry>
<code>$foo->$bar['baz']()</code>
</entry>
<entry>
<code>$foo->{$bar['baz']}()</code>
</entry>
<entry>
<code>($foo->$bar)['baz']()</code>
</entry>
</row>
<row>
<entry>
<code>Foo::$bar['baz']()</code>
</entry>
<entry>
<code>Foo::{$bar['baz']}()</code>
</entry>
<entry>
<code>(Foo::$bar)['baz']()</code>
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
Code that used the old right-to-left evaluation order must be rewritten to
explicitly use that evaluation order with curly braces (see the above
middle column). This will make the code both forwards compatible with PHP
7.x and backwards compatible with PHP 5.x.
</para>
<para>
This also affects the &global; keyword. The curly brace syntax can be
used to emulate the previous behaviour if required:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function f() {
// Valid in PHP 5 only.
global $$foo->bar;
// Valid in PHP 5 and 7.
global ${$foo->bar};
}
?>
]]>
</programlisting>
</informalexample>
</sect3>
<sect3 xml:id="migration70.incompatible.variable-handling.list">
<title>Changes to <function>list</function> handling</title>
<sect4 xml:id="migration70.incompatible.variable-handling.list.order">
<title>
<function>list</function> no longer assigns variables in reverse order
</title>
<para>
<function>list</function> will now assign values to variables in the
order they are defined, rather than reverse order. In general, this only
affects the case where <function>list</function> is being used in
conjunction with the array <code>[]</code> operator, as shown below:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
list($a[], $a[], $a[]) = [1, 2, 3];
var_dump($a);
?>
]]>
</programlisting>
&example.outputs.5;
<screen>
<![CDATA[
array(3) {
[0]=>
int(3)
[1]=>
int(2)
[2]=>
int(1)
}
]]>
</screen>
&example.outputs.7;
<screen>
<![CDATA[
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
]]>
</screen>
</informalexample>
<para>
In general, it is recommended not to rely on the order in which
<function>list</function> assignments occur, as this is an implementation
detail that may change again in the future.
</para>
</sect4>
<sect4 xml:id="migration70.incompatible.variable-handling.list.empty">
<title>Empty <function>list</function> assignments have been removed</title>
<para>
<function>list</function> constructs can no longer be empty. The following
are no longer allowed:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
list() = $a;
list(,,) = $a;
list($x, list(), $y) = $a;
?>
]]>
</programlisting>
</informalexample>
</sect4>
<sect4 xml:id="migration70.incompatible.variable-handling.list.string">
<title><function>list</function> cannot unpack <type>string</type>s</title>
<para>
<function>list</function> can no longer unpack <type>string</type>
variables. <function>str_split</function> should be used instead.
</para>
</sect4>
</sect3>
<sect3 xml:id="migration70.incompatible.variable-handling.array-order">
<title>
Array ordering when elements are automatically created during by reference
assignments has changed
</title>
<para>
The order of the elements in an array has changed when those elements have
been automatically created by referencing them in a by reference
assignment. For example:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$array = [];
$array["a"] =& $array["b"];
$array["b"] = 1;
var_dump($array);
?>
]]>
</programlisting>
&example.outputs.5;
<screen>
<![CDATA[
array(2) {
["b"]=>
&int(1)
["a"]=>
&int(1)
}
]]>
</screen>
&example.outputs.7;
<screen>
<![CDATA[
array(2) {
["a"]=>
&int(1)
["b"]=>
&int(1)
}
]]>
</screen>
</informalexample>
</sect3>
<sect3 xml:id="migration70.incompatible.variable-handling.parentheses">
<title>
Parentheses around function arguments no longer affect behaviour
</title>
<para>
In PHP 5, using redundant parentheses around a function argument could
quiet strict standards warnings when the function argument was passed by
reference. The warning will now always be issued.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
function getArray() {
return [1, 2, 3];
}
function squareArray(array &$a) {
foreach ($a as &$v) {
$v **= 2;
}
}
// Generates a warning in PHP 7.
squareArray((getArray()));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Notice: Only variables should be passed by reference in /tmp/test.php on line 13
]]>
</screen>
</informalexample>
</sect3>
</sect2>
<!-- 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
-->
|