Re: [PHP4BETA] return references branch merged into the main repository

From: Date: Wed, 15 Dec 1999 22:07:29 +0000
Subject: Re: [PHP4BETA] return references branch merged into the main repository
References: 1 2  Groups: php.version4 
Request: Send a blank email to php-version4+get-7977@lists.php.net to get a copy of this message
At 23:53 15/12/1999 , Andrei Zmievski wrote:
On Wed, 15 Dec 1999, Zeev Suraski wrote: Andi and I finished working on the return references support in Zend. We tested it a bit, and merged it into the main branch. To return a reference from a function, you must tag the function as a function that returns a reference: function &foo($a, $b, $c) {
      ...
} However, $a = foo(...) will still not cause $a to be a reference to the returned value (much like $a = $b, when $b is a reference, copies the value of $b onto $a, and doesn't make $a to be a reference to $b). In order to make $a be a reference to the returned value, you must use the notation: $a = &foo(); Can this be changed? This way you have to do double work of declaring function to return reference and remembering to use & when calling it. On the other hand, I guess it gives you flexibility of choosing whether the return value is really a reference. :)
It's partially double work, true, but if you think about it, there's no real other consistent way to do it. First, you don't want to let users take references from functions that don't intend to return references; That's a basic encapsulation rule. So, you must note that you're returning a reference in the function declaration, as the function author. Second, an expression that is a reference, when assigned to a variable, does not create another reference, but a copy of its value: $a = &$foo; /* $a is a reference to $foo */ $b = $a; /* $b is NOT a reference to $foo, it just contains $foo's original value */ $c = &$a; /* $c IS a reference to $foo */ For that reason, if you do $a = foo() even if foo() evaluates to a reference, $a = ref_expr causes $a to be a copy of the value of ref_expr, not a reference to it. Zeev -- Zeev Suraski <zeev@zend.com> http://www.zend.com/

« previous php.version4 (#7977) next »