Return By Reference

User Contributed Notes 22 notes

I ran into something when using an expanded version of the example of pbaltz at NO_SPAM dot cs dot NO_SPAM dot wisc dot edu below.This could be somewhat confusing although it is perfectly clear if you have read the manual carfully. It makes the fact that references always point to the content of a variable perfectly clear (at least to me).

It appears that references can have side-effects. Below are two examples. Both are simply copying one array to another. In the second example, a reference is made to a value in the first array before the copy. In the first example the value at index 0 points to two separate memory locations. In the second example, the value at index 0 points to the same memory location.

I won't say this is a bug, because I don't know what the designed behavior of PHP is, but I don't think ANY developers would expect this behavior, so look out.

An example of where this could cause problems is if you do an array copy in a script and expect on type of behavior, but then later add a reference to a value in the array earlier in the script, and then find that the array copy behavior has unexpectedly changed.

About the example on array references.I think this should be written in the array chapter as well.Indeed if you are new to programming language in some way, you should beware that arrays are pointers to a vector of Byte(s).

<?php $arr = array(1); ?>$arr here contains a reference to which the array is located.Writing :<?php echo $arr[0]; ?>dereferences the array to access its very first element.

Now something that you should also be aware of (even you are not new to programming languages) is that PHP use references to contains the different values of an array. And that makes sense because the type of the elements of a PHP array can be different.

When there is a constructor, the strange behavior mentioned in my last post doesn't occur. My guess is that php was treating reftest() as a constructor (maybe because it was the first function?) and running it upon instantiation.

Something that might not be obvious on the first look:If you want to cycle through an array with references, you must not use a simple value assigning foreach control structure. You have to use an extended key-value assigning foreach or a for control structure.

A simple value assigning foreach control structure produces a copy of an object or value. The following code

to reply to ' elrah [] polyptych [dot] com ', one thing to keep in mind is that array (or similar large data holders) are by default passed by reference. So the behaviour is not side effect. And for array copy and passing array inside function always done by 'pass by reference'...

When a variable is copied, a reference is used internally until the copy is modified. Therefore you shouldn't use references at all in your situation as it doesn't save any memory usage and increases the chance of logic bugs, as you discoved.

In this example class name is different from its first function and however there is no construction function. In the end as you guess "a" and "c" are equal. So if there is no construction function at same time class and its first function names are the same, "a" and "c" doesn't equal forever. In my opinion php doesn't seek any function for the construction as long as their names differ from each others.

This appears to be the hidden behavior: When a class function has the same name as the class, it seems to be implicitly called when an object of the class is created.For instance, you may take a look at the naming of the function "reftest()": it is in the class "reftest". The behavior can be tested as follows:

<?phpclass reftest{ public $a = 1; public $c = 1;

public function reftest1() {$b =& $this->a;$b++; }

public function reftest2() {$d =& $this->c;$d++; }

public function reftest() { echo "REFTEST() called here!\n"; }}

$reference = new reftest();/*You must notice the above will also implicitly call reference->reftest()*/

Something that has not been discussed so far is reference of a reference.I needed a quick and dirty method of aliasing incorrect naming until a proper rewrite could be done.Hope this saves someone else the time of testing since it was not covered in the Does/Are/Are Not pages.Far from best practice, but it worked.

An interesting if offbeat use for references: Creating an array with an arbitrary number of dimensions.

For example, a function that takes the result set from a database and produces a multidimensional array keyed according to one (or more) columns, which might be useful if you want your result set to be accessible in a hierarchial manner, or even if you just want your results keyed by the values of each row's primary/unique key fields.

<?phpfunction array_key_by($data, $keys, $dupl = false)/* * $data - Multidimensional array to be keyed * $keys - List containing the index/key(s) to use. * $dupl - How to handle rows containing the same values. TRUE stores it as an Array, FALSE overwrites the previous row. * * Returns a multidimensional array indexed by $keys, or NULL if error. * The number of dimensions is equal to the number of $keys provided (+1 if $dupl=TRUE). */ {// Sanity checkif (!is_array($data)) return null;

// Bind $last to one dimension 'deeper'. // First lap: was &$out, now &$out[...] // Second lap: was &$out[...], now &$out[...][...] // Third lap: was &$out[...][...], now &$out[...][...][...] // (etc.)$last =& $last[$value]; }