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; for instance,
you cannot perform pointer arithmetic using them, they are not
actual memory addresses, and so on. See
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.
What References Do
There are three basic operations performed using references:
assigning by
reference, passing
by reference,
and returning by
reference. This section will give an introduction to these
operations, with links for further reading.
Assign By Reference
In the first of these, PHP references allow you to make two
variables refer to the same content. Meaning, when you do:
]]>
it means that $a and $b
point to the same content.
$a and $b are completely
equal here. $a is not pointing to
$b or vice versa.
$a and $b are pointing to the
same place.
If you assign, pass, or return an undefined variable by reference,
it will get created.
Using references with undefined variables
d);
var_dump(property_exists($c, 'd')); // bool(true)
?>
]]>
The same syntax can be used with functions that return
references:
]]>
Using the same syntax with a function that does not
return by reference will give an error, as will using it with the result
of the new operator.
Although objects are passed around as pointers, these are not the same as references,
as explained under Objects and references.
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 functions
]]>
Think about global $var; as a shortcut to $var
=& $GLOBALS['var'];. Thus assigning another 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
]]>
While not being strictly an assignment by reference, expressions created
with the language construct
array() can also
behave as such by prefixing & to the array element
to add. Example:
]]>
Note, however, that references inside arrays are potentially dangerous.
Doing a normal (not by reference) assignment with a reference on the
right side does not turn the left side into a reference, but references
inside arrays are preserved in these normal assignments. This also applies
to function calls where the array is passed by value. Example:
]]>
In other words, the reference behavior of arrays is defined in an
element-by-element basis; the reference behavior of individual elements
is dissociated from the reference status of the array container.
Pass By Reference
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:
]]>
will make $a to be 6. This happens because in
the function foo the variable
$var refers to the same content as
$a. For more information on this, read
the passing by
reference section.
Return By Reference
The third thing references can do is return by reference.
What References Are Not
As said before, references are not 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 the caller, but then
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 a variable by reference to a function so the function
can modify the variable. The syntax is as follows:
]]>
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.
The following things can be passed by reference:
Variables, i.e. foo($a)
References returned from functions, i.e.:
]]>
See more about returning by reference.
No other expressions should be passed by reference, as the
result is undefined. For example, the following examples of passing
by reference are invalid:
]]>
Returning References
Returning by reference is useful when you want to use a function
to find to which variable a reference should be bound. Do
not 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:
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.
?>
]]>
In this example, the property of the object returned by the
getValue 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
want to return by reference, not a copy, and to indicate that
reference binding, rather than usual assignment, should be done
for $myValue.
If you try to return a reference from a function with the syntax:
return ($this->value); this will not
work as you are attempting to return the result of an
expression, and not a variable, by reference. You can
only return variables by reference from a function - nothing else.
To use the returned reference, you must use reference assignment:
]]>
To pass the returned reference to another function expecting a reference
you can use this syntax:
]]>
Note that array_push(&collector(), 'foo'); will
not work, it results in a fatal error.
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 the Unix
unlink call.
Spotting References
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:
global References
When you declare a variable as global $var you
are in fact creating reference to a global variable. That means,
this is the same as:
]]>
This also means that unsetting $var
won't unset the global variable.