language/references.xml
6d29533483657c036e49edb5ea88c7103d126681
...
...
@@ -74,7 +74,7 @@ $b = array();
74
74
foo($b['b']);
75
75
var_dump(array_key_exists('b', $b)); // bool(true)
76
76

77
-
$c = new StdClass;
77
+
$c = new stdClass;
78
78
foo($c->d);
79
79
var_dump(property_exists($c, 'd')); // bool(true)
80
80
?>
...
...
@@ -85,8 +85,7 @@ var_dump(property_exists($c, 'd')); // bool(true)
85
85
</note>
86
86
<para>
87
87
The same syntax can be used with functions that return
88
-
references, and with the <literal>new</literal> operator (since PHP
89
-
4.0.4 and before PHP 5.0.0):
88
+
references:
90
89
<informalexample>
91
90
<programlisting role="php">
92
91
<![CDATA[
...
...
@@ -96,17 +95,13 @@ $foo =& find_var($bar);
96
95
]]>
97
96
</programlisting>
98
97
</informalexample>
99
-
Since PHP 5, <link linkend="language.oop5.basic.new">new</link>
100
-
returns a reference automatically, so
101
-
using <literal>=&amp;</literal> in this context is deprecated and
102
-
produces an <constant>E_DEPRECATED</constant> message in PHP 5.3 and
103
-
later, and an <constant>E_STRICT</constant> message in earlier versions.
104
-
As of PHP 7.0 it is syntactically invalid.
105
-
(Technically, the difference is that, in PHP 5, object variables, much like
106
-
resources, are a mere pointer to the actual object data, so these object
107
-
references are not "references" in the same sense used before (aliases).
108
-
For more information, see <link linkend="language.oop5.references">Objects
109
-
and references</link>.)
98
+
</para>
99
+
<para>
100
+
Using the same syntax with a function that does <emphasis>not</emphasis>
101
+
return by reference will give an error, as will using it with the result
102
+
of the <link linkend="language.oop5.basic.new">new</link> operator.
103
+
Although objects are passed around as pointers, these are not the same as references,
104
+
as explained under <link linkend="language.oop5.references">Objects and references</link>.
110
105
</para>
111
106
<warning>
112
107
<para>
...
...
@@ -316,11 +311,7 @@ foo($a);
316
311
<simpara>
317
312
There is no reference sign on a function call - only on
318
313
function definitions. Function definitions alone are enough to
319
-
correctly pass the argument by reference. As of PHP 5.3.0,
320
-
you will get a warning saying that "call-time pass-by-reference" is
321
-
deprecated when you use &amp; in <literal>foo(&amp;$a);</literal>.
322
-
And as of PHP 5.4.0, call-time pass-by-reference was removed, so
323
-
using it will raise a fatal error.
314
+
correctly pass the argument by reference.
324
315
</simpara>
325
316
</note>
326
317
</para>
...
...
@@ -333,11 +324,6 @@ foo($a);
333
324
</simpara>
334
325
</listitem>
335
326
<listitem>
336
-
<simpara>
337
-
New statements, i.e. <literal>foo(new foobar())</literal>
338
-
</simpara>
339
-
</listitem>
340
-
<listitem>
341
327
<para>
342
328
References returned from functions, i.e.:
343
329
<informalexample>
...
...
@@ -381,11 +367,17 @@ function bar() // Note the missing &
381
367
$a = 5;
382
368
return $a;
383
369
}
384
-
foo(bar()); // Produces fatal error as of PHP 5.0.5, strict standards notice
385
-
// as of PHP 5.1.1, and notice as of PHP 7.0.0
370
+
foo(bar()); // Produces a notice
386
371

387
372
foo($a = 5); // Expression, not variable
388
373
foo(5); // Produces fatal error
374
+

375
+
class Foobar
376
+
{
377
+
}
378
+

379
+
foo(new Foobar()) // Produces a notice as of PHP 7.0.7
380
+
// Notice: Only variables should be passed by reference
389
381
?>
390
382
]]>
391
383
</programlisting>
...
...
@@ -442,13 +434,10 @@ echo $myValue; // prints the new value of $obj->value, i.e. 2.
442
434
work as you are attempting to return the result of an
443
435
<emphasis>expression</emphasis>, and not a variable, by reference. You can
444
436
only return variables by reference from a function - nothing else.
445
-
Since PHP 5.1.0, an
446
-
<constant>E_NOTICE</constant> error is issued if the code tries to return
447
-
a dynamic expression or a result of the <literal>new</literal> operator.
448
437
</simpara>
449
438
</note>
450
439
<para>
451
-
To use the returned reference, you must use reference assigment:
440
+
To use the returned reference, you must use reference assignment:
452
441
<informalexample>
453
442
<programlisting role="php">
454
443
<![CDATA[
...
...
@@ -543,14 +532,6 @@ $var =& $GLOBALS["var"];
543
532
won't unset the global variable.
544
533
</simpara>
545
534
</sect2>
546
-

547
-
<sect2 xml:id="references.this">
548
-
<title><literal>$this</literal></title>
549
-
<simpara>
550
-
In an object method, <varname>$this</varname> is always a reference
551
-
to the caller object.
552
-
</simpara>
553
-
</sect2>
554
535
</sect1>
555
536

556
537
</chapter>
557
538