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,16 +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
-
(Technically, the difference is that, in PHP 5, object variables, much like
105
-
resources, are a mere pointer to the actual object data, so these object
106
-
references are not "references" in the same sense used before (aliases).
107
-
For more information, see <link linkend="language.oop5.references">Objects
108
-
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>.
109
105
</para>
110
106
<warning>
111
107
<para>
...
...
@@ -315,11 +311,7 @@ foo($a);
315
311
<simpara>
316
312
There is no reference sign on a function call - only on
317
313
function definitions. Function definitions alone are enough to
318
-
correctly pass the argument by reference. As of PHP 5.3.0,
319
-
you will get a warning saying that "call-time pass-by-reference" is
320
-
deprecated when you use &amp; in <literal>foo(&amp;$a);</literal>.
321
-
And as of PHP 5.4.0, call-time pass-by-reference was removed, so
322
-
using it will raise a fatal error.
314
+
correctly pass the argument by reference.
323
315
</simpara>
324
316
</note>
325
317
</para>
...
...
@@ -332,11 +324,6 @@ foo($a);
332
324
</simpara>
333
325
</listitem>
334
326
<listitem>
335
-
<simpara>
336
-
New statements, i.e. <literal>foo(new foobar())</literal>
337
-
</simpara>
338
-
</listitem>
339
-
<listitem>
340
327
<para>
341
328
References returned from functions, i.e.:
342
329
<informalexample>
...
...
@@ -380,15 +367,21 @@ function bar() // Note the missing &
380
367
$a = 5;
381
368
return $a;
382
369
}
383
-
foo(bar()); // Produces fatal error since PHP 5.0.5
370
+
foo(bar()); // Produces a notice
384
371

385
372
foo($a = 5); // Expression, not variable
386
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
387
381
?>
388
382
]]>
389
383
</programlisting>
390
384
</informalexample>
391
-
These requirements are for PHP 4.0.4 and later.
392
385
</para>
393
386
</sect1>
394
387

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

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

555
537
</chapter>
556
538