PrimitiveType

PHP references

This article looks at how to use references in PHP 4. As PHP 4 handles references in a quirky and unique way, this is something all PHP coders should get to grips with!

The "&" operator is used to indicate that a variable should be assigned or passed by reference, depending on the context. References in PHP are not like pointers in C. Instead, references are used to make variable names aliases of other variable names, so that they both refer to the same contents.

Assignment by reference

When a variable is assigned to another variable, it is actually assigned to a copy of the value of the variable (at least from the programmer's point of view; the PHP interpreter can make optimizations so that a copy is only made when it is actually needed). Consider the following snippet:

$a = 100; $b = $a; $a++;

At the end of the snippet, the value of $a is 101, while the value of $b remains at 100. Next, consider a similar snippet:

$a = 100; $b =& $a; $a++;

Now, the value of both $a and $b is 101. This is because the assignment by reference means that both variable names refer to the same content, so that if any of the names are used in an operation to modify the content, the modified content is also linked to the other variable name.

In PHP 4, when you use "new" to create a new object and assign it to a variable with "=", a copy of the newly created object is actually assigned to the variable. Much of the time you won't notice any effects, but to avoid any possible problems, you should use "=&":

$myObj = new MyClass();// Creates copy of new object $myObj =& new MyClass();// Returns reference of new object

Note that using "=& new MyClass()" is deprecated in PHP 5, in which "new" returns a reference by default.

Pass by reference

When a function is to modify a variable that is passed to it, it should be passed by reference - otherwise, a local copy of the variable will be made in the function and the changes won't affect the variable passed from outside. To indicate that a function should accept a reference as an argument, use the "&" operator next to the argument name in the function definition. Several built-in PHP functions take references, such as the function sort(), which sorts the elements of an array. This is the description of the sort() function:

bool sort ( array &$array [, int $sort_flags] )

If you pass an array with the elements 3, 2, 1 to sort(), it will be rearranged as 1, 2, 3:

$a = array(3, 2, 1); sort($a); print_r($a);

Outputs:

Array ( [0] => 1 [1] => 2 [2] => 3 )

Notice that you do not need to include the "&" next to the variable name when calling the function, as it is already included in the function definition. If you would like to pass a variable by reference to a function that usually takes a copy, you can do so by prefixing the variable name with "&", but this is probably to be discouraged as the effects are not always predictable or well described in the function documentation. The PHP manual states that you should not pass values by reference to speed things up, as the necessary optimizations are already done by the PHP interpreter. Also, using "&" when calling a function is deprecated in later versions of PHP and will generate a warning.

Return a reference from a function

In PHP 4, all functions that return an object should do so by reference - otherwise a copy of the object, possibly just created in the function, is needlessly returned instead. To indicate that a function should return a reference, prefix the function name with a "&".

function &getPerson($personID) { $person =& new Person($personID); return $person; } $person =& getPerson(123);

Notice that the assignment of the reference returned by getPerson() still needs to use the "&" to avoid obtaining a copy of the object.