NYCPHP Meetup

NYPHP.org

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

Urb LeJeune urb at e-government.com
Wed Nov 21 09:33:14 EST 2007


>They stated, if you know your not going to make changes
>to a variable, to send it to a function as reference, as to NOT
>make a copy of it...

         There are two types of calls.

1. Call by value
2. Call by reference

         Unless PHP does things differently than any other language that
I have ever used, when a function is call with call by value reference
a local copy is made for every variable. You can change the value of
the local variable and the value of the variable in the calling portion of
the program does not change.

         On the other hand, with a call by reference the address of the
variable is passed, not the value(s) of that variable or array. Using
call by reference there is no local copy of the value(s) in the function.
If you're passing a large array there is slight increase in efficiency
when passing by reference because the function does not have to
make a second copy of the array and clean up and reallocate the
memory at the end of the function.

         By convention I always pass array function arguments using
a call be reference.




>Is this really worthwhile?
>
>func($a)
>{
>    echo $a;
>}
>
>VS
>
>func(& $a)
>{
>    echo $a;
>}
>
>- Ben
>
>----- Original Message ----- From: "Cliff Hirsch" <cliff at pinestream.com>
>To: "NYPHP Talk" <talk at lists.nyphp.org>
>Sent: Monday, November 19, 2007 3:01 PM
>Subject: Re: [nycphp-talk] Why is pass-by-reference deprecated?
>
>
>On 11/19/07 1:27 PM, "Gary Mort" <bz-gmort at beezifies.com> wrote:
>>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);
>
>Ah, I got it. Pass-by-reference in the function call is what's depricated.
>As in foo(&$a); (as you noted above).
>
>Pass-by-reference in the function definition is not depricated. As in
>public function Thefunction(&$varref) {
>}
>
>Still ugly and error prone compared to clean oop, but passing objects around
>is sort of the same thing and infinitely more confusing.
>
>
>_______________________________________________
>New York PHP Community Talk Mailing List
>http://lists.nyphp.org/mailman/listinfo/talk
>
>NYPHPCon 2006 Presentations Online
>http://www.nyphpcon.com
>
>Show Your Participation in New York PHP
>http://www.nyphp.org/show_participation.php
>_______________________________________________
>New York PHP Community Talk Mailing List
>http://lists.nyphp.org/mailman/listinfo/talk
>
>NYPHPCon 2006 Presentations Online
>http://www.nyphpcon.com
>
>Show Your Participation in New York PHP
>http://www.nyphp.org/show_participation.php

Urb

Dr. Urban A. LeJeune, President
E-Government.com
800-204-9545





More information about the talk mailing list