What 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:
$a =& $b
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.
The same syntax can be used with functions, that return references, and with new operator (in PHP 4.0.4 and later):
$bar =& new fooclass();
$foo =& find_var ($bar);
Замечание: 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.
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:
function foo (&$var)
{
$var++;
}
$a=5;
foo ($a);
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:
function foo (&$var)
{
$var =& $GLOBALS["baz"];
}
foo($bar);
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).
Passing by Reference
You can pass variable to function by reference, so that function could modify its arguments. The syntax is as follows:
function foo (&$var)
{
$var++;
}
$a=5;
foo ($a);
// $a is 6 here
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.:
function &bar()
{
$a = 5;
return $a;
}
foo(bar());
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:
function bar() // Note the missing &
{
$a = 5;
return $a;
}
foo(bar());
foo($a = 5) // Expression, not variable
foo(5) // Constant, not variable
These requirements are for PHP 4.0.4 and later.
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:
$a = 1;
$b =& $a;
unset ($a);
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:
$var =& $GLOBALS["var"];
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.