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

From: Date: Wed, 15 Dec 1999 22:16:25 +0000
Subject: Re: [PHP4BETA] return references branch merged into the main repository
References: 1 2 3 4 5  Groups: php.version4 
Request: Send a blank email to php-version4+get-7979@lists.php.net to get a copy of this message
At 00:07 16/12/1999 , Andrei Zmievski wrote:
On Thu, 16 Dec 1999, Zeev Suraski wrote: 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. Right, & in function declaration should stay there. 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. So what you're saying is there is no way to tell whether foo() returns reference when it's being executed?
No, that's not what I'm saying. If you use no special notation, foo() is a standalone expression, not different from $var. When you assign this expression to another variable, if you assign it using the standard assignment notation, it'll create a copy of whatever that expression was (foo() could have returned a reference, and $var could have been a reference, but when you assign them to some variable, that variable, no matter what, will not be a reference to them but a copy of them). To assign a reference, you must use the special assign-reference notation. When assigning a variable, the notation does the job of converting the variable to a reference if necessary. When assigning a function return value, you must first have a function that returns a reference (because we don't want users to take references from functions that aren't designed for that), and then still, say that you want to actually take a reference from that reference, and not create a copy of it. It's just a matter of consistency. Zeev -- Zeev Suraski <zeev@zend.com> http://www.zend.com/

« previous php.version4 (#7979) next »