File: references.xml

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (485 lines) | stat: -rw-r--r-- 14,257 bytes parent folder | download
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297028 $ -->
 <chapter xml:id="language.references" xmlns="http://docbook.org/ns/docbook">
  <title>References Explained</title>

  <sect1 xml:id="language.references.whatare">
   <title>What References Are</title>
   <simpara>
    References in PHP are a means to access the same variable content
    by different names. They are not like C pointers; for instance,
    you cannot perform pointer arithmetic using them, they are not
    actual memory addresses, and so on. See
    <xref linkend="language.references.arent" /> for more
    information. Instead, they are symbol table aliases. Note that in
    PHP, variable name and variable content are different, so the same
    content can have different names.  The closest analogy is with
    Unix filenames and files - variable names are directory entries,
    while variable content is the file itself. References can be
    likened to hardlinking in Unix filesystem.
   </simpara>
  </sect1>

  <sect1 xml:id="language.references.whatdo">
   <title>What References Do</title>
   <para>
    There are three basic operations performed using references:
    <link linkend="language.references.whatdo.assign">assigning by
    reference</link>, <link linkend="language.references.whatdo.pass">passing
    by reference</link>,
    and <link linkend="language.references.whatdo.return">returning by
    reference</link>. This section will give an introduction to these
    operations, with links to further reading.
   </para>
   <sect2 xml:id="language.references.whatdo.assign">
    <title>Assign By Reference</title>
    <para>
     In the first of these, PHP references allow you to make two
     variables refer to the same content. Meaning, when you do:
     <informalexample>
      <programlisting role="php">
<![CDATA[
<?php
$a =& $b;
?>
]]>
      </programlisting>
     </informalexample>
     it means that <varname>$a</varname> and <varname>$b</varname>
     point to the same content.
     <note>
      <para>
       <varname>$a</varname> and <varname>$b</varname> are completely
       equal here. <varname>$a</varname> is not pointing to
       <varname>$b</varname> or vice versa.
       <varname>$a</varname> and <varname>$b</varname> are pointing to the
       same place.
      </para>
     </note>
    </para>
    <note>
     <para>
      If arrays with references are copied, their values are not dereferenced.
      This is valid also for arrays passed by value to functions.
     </para>
    </note>
    <note>
     <para>
      If you assign, pass, or return an undefined variable by reference, 
      it will get created.
      <example>
       <title>Using references with undefined variables</title>
       <programlisting role="php">
<![CDATA[
<?php
function foo(&$var) { }

foo($a); // $a is "created" and assigned to null

$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)

$c = new StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)
?>
]]>
       </programlisting>
      </example>
     </para>
    </note>
    <para>
     The same syntax can be used with functions that return
     references, and with the <literal>new</literal> operator (since PHP
     4.0.4 and before PHP 5.0.0):
     <informalexample>
      <programlisting role="php">
<![CDATA[
<?php
$foo =& find_var($bar);
?>
]]>
      </programlisting>
     </informalexample>
     Since PHP 5, <link linkend="language.oop5.basic.new">new</link>
     returns a reference automatically, so
     using <literal>=&amp;</literal> in this context is deprecated and
     produces an <constant>E_STRICT</constant> message.
    </para>
    <warning>
     <para>
      If you assign a reference to a variable declared <literal>global</literal>
      inside a function, the reference will be visible only inside the function.
      You can avoid this by using the <varname>$GLOBALS</varname> array.
      <example>
       <title>Referencing global variables inside functions</title>
       <programlisting role="php">
<![CDATA[
<?php
$var1 = "Example variable";
$var2 = "";

function global_references($use_globals)
{
    global $var1, $var2;
    if (!$use_globals) {
        $var2 =& $var1; // visible only inside the function
    } else {
        $GLOBALS["var2"] =& $var1; // visible also in global context
    }
}

global_references(false);
echo "var2 is set to '$var2'\n"; // var2 is set to ''
global_references(true);
echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
?>
]]>
       </programlisting>
      </example>
      Think about <literal>global $var;</literal> as a shortcut to <literal>$var
        =&amp; $GLOBALS['var'];</literal>. Thus assigning another reference
      to <literal>$var</literal> only changes the local variable's reference.
     </para>
    </warning>
    <note>
     <para>
      If you assign a value to a variable with references in a 
      &foreach; statement, the references are modified too.
      <example>
       <title>References and foreach statement</title>
       <programlisting role="php">
<![CDATA[
<?php
$ref = 0;
$row =& $ref;
foreach (array(1, 2, 3) as $row) {
    // do something
}
echo $ref; // 3 - last element of the iterated array
?>
]]>
       </programlisting>
      </example>
     </para>
    </note>
   </sect2>
   <sect2 xml:id="language.references.whatdo.pass">
    <title>Pass By Reference</title>
    <para>
     The second thing references do is to pass variables by
     reference. This is done by making a local variable in a function
     and a variable in the calling scope referencing the same
     content. Example:
     <informalexample>
      <programlisting role="php">
<![CDATA[
<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
?>
]]>
      </programlisting>
     </informalexample>
     will make <varname>$a</varname> to be 6. This happens because in
     the function <varname>foo</varname> the variable
     <varname>$var</varname> refers to the same content as
     <varname>$a</varname>. For more information on this, read
     the <link linkend="language.references.pass">passing by
       reference</link> section.
    </para>
   </sect2>
   <sect2 xml:id="language.references.whatdo.return">
    <title>Return By Reference</title>
    <para>
     The third thing references can do is <link
     linkend="language.references.return">return by reference</link>.
    </para>
   </sect2>
  </sect1>

  <sect1 xml:id="language.references.arent">
   <title>What References Are Not</title>
   <para>
    As said before, references are not pointers. That means, the
    following construct won't do what you expect:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
function foo(&$var)
{
    $var =& $GLOBALS["baz"];
}
foo($bar); 
?>
]]>
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    What happens is that <varname>$var</varname> in
    <varname>foo</varname> will be bound with
    <varname>$bar</varname> in the caller, but then
    re-bound with <varname>$GLOBALS["baz"]</varname>. There's no way
    to bind <varname>$bar</varname> in the calling scope to something else
    using the reference mechanism, since <varname>$bar</varname> is not
    available in the function <varname>foo</varname> (it is represented by
    <varname>$var</varname>, but <varname>$var</varname> has only
    variable contents and not name-to-value binding in the calling
    symbol table).
    You can use <link linkend="language.references.return">returning
    references</link> to reference variables selected by the function.
   </simpara>
  </sect1>

  <sect1 xml:id="language.references.pass">
   <title>Passing by Reference</title>
   <para>
   You can pass a variable by reference to a function so the function
   can modify the variable. The syntax is as follows:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?>
]]>
     </programlisting>
    </informalexample>
    <note>
     <simpara>
      There is no reference sign on a function call - only on
      function definitions. Function definitions alone are enough to
      correctly pass the argument by reference.  As of PHP 5.3.0,
      you will get a warning saying that "call-time pass-by-reference" is
      deprecated when you use &amp; in <literal>foo(&amp;$a);</literal>.
     </simpara>
    </note>
  </para>
  <para>
  The following things can be passed by reference:
   <itemizedlist>
    <listitem>
     <simpara>
      Variables, i.e. <literal>foo($a)</literal>
     </simpara>
    </listitem>
    <listitem>
     <simpara>
      New statements, i.e. <literal>foo(new foobar())</literal>
     </simpara>
    </listitem>
    <listitem>
     <para>
      References returned from functions, i.e.:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
function foo(&$var)
{
    $var++;
}
function &bar()
{
    $a = 5;
    return $a;
}
foo(bar());
?>
]]>
     </programlisting>
    </informalexample>
    See more about <link
    linkend="language.references.return">returning by reference</link>. 
     </para>
    </listitem>
  </itemizedlist>
  </para>
  <para>
  No other expressions should be passed by reference, as the
  result is undefined. For example, the following examples of passing
  by reference are invalid:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
function foo(&$var)
{
    $var++;
}
function bar() // Note the missing &
{
    $a = 5;
    return $a;
}
foo(bar()); // Produces fatal error since PHP 5.0.5

foo($a = 5); // Expression, not variable
foo(5); // Produces fatal error
?>
]]>
     </programlisting>
    </informalexample>
    These requirements are for PHP 4.0.4 and later.
   </para>
  </sect1>

  <sect1 xml:id="language.references.return">
   <title>Returning References</title>
   <para>
    Returning by reference is useful when you want to use a function
    to find to which variable a reference should be bound. Do
    <emphasis>not</emphasis> use return-by-reference to increase performance.
    The engine will automatically optimize this on its own. Only return
    references when you have a valid technical reason to do so. To
    return references, use this syntax:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
class foo {
    public $value = 42;

    public function &getValue() {
        return $this->value;
    }
}

$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;                // prints the new value of $obj->value, i.e. 2.
?> 
]]>
     </programlisting>
    </informalexample>
    In this example, the property of the object returned by the
    <varname>getValue</varname> function would be set, not the
    copy, as it would be without using reference syntax.
   </para>
   <note>
    <simpara>
     Unlike parameter passing, here you have to use
     <literal>&amp;</literal> in both places - to indicate that you
     want to return by reference, not a copy, and to indicate that
     reference binding, rather than usual assignment, should be done
     for <varname>$myValue</varname>.
    </simpara>
   </note>
   <note>
    <simpara>
     If you try to return a reference from a function with the syntax:
     <literal>return ($this->value);</literal> this will <emphasis>not</emphasis>
     work as you are attempting to return the result of an
     <emphasis>expression</emphasis>, and not a variable, by reference. You can
     only return variables by reference from a function - nothing else.
     Since PHP 4.4.0 in the PHP4 branch, and PHP 5.1.0 in the PHP5 branch, an
     <constant>E_NOTICE</constant> error is issued if the code tries to return
     a dynamic expression or a result of the <literal>new</literal> operator.
    </simpara>
   </note>
  </sect1>

  <sect1 xml:id="language.references.unset">
   <title>Unsetting References</title>
   <para>
    When you unset the reference, you just break the binding between
    variable name and variable content. This does not mean that
    variable content will be destroyed. For example:
    <informalexample>
     <programlisting role="php">
<![CDATA[
<?php
$a = 1;
$b =& $a;
unset($a); 
?>
]]>
     </programlisting>
    </informalexample>
    won't unset <varname>$b</varname>, just <varname>$a</varname>. 
   </para>
   <simpara>
    Again, it might be useful to think about this as analogous to the Unix
    <command>unlink</command> call.
   </simpara>
  </sect1>

  <sect1 xml:id="language.references.spot">
   <title>Spotting References</title>
   <simpara>
    Many syntax constructs in PHP are implemented via referencing
    mechanisms, so everything mentioned herein about reference binding also
    applies to these constructs. Some constructs, like passing and
    returning by reference, are mentioned above. Other constructs that
    use references are:
   </simpara>

   <sect2 xml:id="references.global">
    <title>global References</title>
    <para>
     When you declare a variable as <command>global $var</command> you
     are in fact creating reference to a global variable. That means,
     this is the same as:
     <informalexample>
      <programlisting role="php">
<![CDATA[
<?php
$var =& $GLOBALS["var"];
?>
]]>
      </programlisting>
     </informalexample>
    </para>
    <simpara>
     This also means that unsetting <varname>$var</varname>
     won't unset the global variable.
    </simpara>
   </sect2>

   <sect2 xml:id="references.this">
    <title><literal>$this</literal></title>
    <simpara>
     In an object method, <varname>$this</varname> is always a reference
     to the caller object.
    </simpara>
   </sect2>
  </sect1>

 </chapter>
 
<!-- 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
-->