References ExplainedWhat References Are
References in PHP are a means to access the same variable content
by different names. They are not like C pointers, 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 most close analogy is with Unix filenames and files -
variable names are directory entries, while variable contents is
the file itself. References can be thought of as hardlinking in
Unix filesystem.
What References Do
PHP references allow you to make two variables to refer to the
same content. Meaning, when you do:
]]>
it means that $a and $b
point to the same variable.
$a and $b are completely
equal here, that's not $a is pointing to
$b or vice versa, that's
$a and $b pointing to the
same place.
If array with references is copied, its values are not dereferenced.
This is valid also for arrays passed by value to functions.
The same syntax can be used with functions, that return references,
and with new operator (in PHP 4.0.4 and later):
]]>
Not using the & operator causes a copy of the
object to be made. If you use $this in the class it
will operate on the current instance of the class. The assignment without
& will copy the instance (i.e. the object) and
$this will operate on the copy, which is not always
what is desired. Usually you want to have a single instance to work with,
due to performance and memory consumption issues.
While you can use the @ operator to
mute any errors in the constructor when using it as
@new, this does not work when using the
&new statement. This is a limitation of the Zend
Engine and will therefore result in a parser error.
If you assign a reference to a variable declared global
inside a function, the reference will be visible only inside the function.
You can avoid this by using the $GLOBALS array.
Referencing global variables inside function
]]>
Think about global $var; as a shortcut to $var
=& $GLOBALS['var'];. Thus assigning other reference
to $var only changes the local variable's reference.
If you assign a value to a variable with references in a foreach statement,
the references are modified too.
References and foreach statement
]]>
Complex arrays are sometimes rather copied than referenced. Thus following
example will not work as expected.
References with complex arrays
array(),
'B' => array(
'B_b' => array(),
),
);
$top['A']['parent'] = &$top;
$top['B']['parent'] = &$top;
$top['B']['B_b']['data'] = 'test';
print_r($top['A']['parent']['B']['B_b']); // array()
?>
]]>
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 reference to the same content. Example:
]]>
will make $a to be 6. This happens because in
the function foo the variable
$var refers to the same content as
$a. See also more detailed explanations about passing by reference.
The third thing reference can do is return by reference.
What References Are Not
As said before, references aren't pointers. That means, the
following construct won't do what you expect:
]]>
What happens is that $var in
foo will be bound with
$bar in caller, but then it will be
re-bound with $GLOBALS["baz"]. There's no way
to bind $bar in the calling scope to something else
using the reference mechanism, since $bar is not
available in the function foo (it is represented by
$var, but $var has only
variable contents and not name-to-value binding in the calling
symbol table).
You can use returning
references to reference variables selected by the function.
Passing by Reference
You can pass variable to function by reference, so that function could modify
its arguments. The syntax is as follows:
]]>
Note that there's no reference sign on function call - only on
function definition. Function definition alone is enough to
correctly pass the argument by reference.
Following things can be passed by reference:
Variable, i.e. foo($a)
New statement, i.e. foo(new foobar())
Reference, returned from a function, i.e.:
]]>
See also explanations about returning by reference.
Any other expression should not be passed by reference, as the
result is undefined. For example, the following examples of passing
by reference are invalid:
]]>
These requirements are for PHP 4.0.4 and later.
Returning References
Returning by-reference is useful when you want to use a function
to find which variable a reference should be bound to. When
returning references, use this syntax:
x = 2;
?>
]]>
In this example, the property of the object returned by the
find_var function would be set, not the
copy, as it would be without using reference syntax.
Unlike parameter passing, here you have to use
& in both places - to indicate that you
return by-reference, not a copy as usual, and to indicate that
reference binding, rather than usual assignment, should be done
for $foo.
Unsetting References
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:
]]>
won't unset $b, just $a.
Again, it might be useful to think about this as analogous to Unix
unlink call.
Spotting References
Many syntax constructs in PHP are implemented via referencing
mechanisms, so everything told above about reference binding also
apply to these constructs. Some constructs, like passing and
returning by-reference, are mentioned above. Other constructs that
use references are:
global References
When you declare variable as global $var you
are in fact creating reference to a global variable. That means,
this is the same as:
]]>
That means, for example, that unsetting $var
won't unset global variable.
$this
In an object method, $this is always reference
to the caller object.