25 April 2012

Working with References

Starting in PHP 5.3.0, a lot of people began having issues with code written for previous versions. The reason for this is because a feature known as call-time pass-by-reference was deprecated. For the sake of consistency, the ability to add the reference symbol (&) to a function call (myFunc( &$a )) was deprecated.

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. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);.

See: PHP: References

When you work with a large code-base, and you required the reference to be passed at the function call level, your resulting variable will be skewed if you don't reference the variable on call. By moving the reference notation to the function definition, you remove the possibility of user error when calling your function, ensuring the reference is passed every time the function is called.



function myFunc( &$arg ) { 
    // do something

myFunc( $var );


function myFunc($arg) { 
    // do something

myFunc( &$va r);