NYCPHP Meetup

NYPHP.org

[nycphp-talk] Why is pass-by-reference deprecated?

Gary Mort bz-gmort at beezifies.com
Mon Nov 19 13:27:04 EST 2007


Cliff Hirsch wrote:
> The php manual says:
>
> “In recent versions of PHP you will get a warning saying that 
> "Call-time pass-by-reference" is deprecated when you use a & in foo(&$a);”
>
> Why is this? Besides being ugly, difficult to understand and not very 
> elegant, is there any reason technical reason why this is deprecated?
>

Because if you declare it in the function:

function foo(&$mya) {
}

Than you have told PHP that whenever this function is used, variables 
should be passed by reference and not copied.

So the thinking is, you should know ahead of time whether or not you 
want to pass by reference or pass a copy, and not decide to do it at the 
time you call your code.

IE, don't do:
foo(&$a);
echo $a;
foo($b);
echo $b;

Where $a is changed by foo and $b is not.

If you must have function that does one thing or the other, create 2 
function:
function foo(&$a) {
}

function foosafe($a) {
foo($a);
}





More information about the talk mailing list