Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration

From: Date: Fri, 13 Apr 2012 21:45:47 +0000
Subject: Re: [RFC] Allow "use( $longname as $alias)" syntax for closures declaration
References: 1 2 3 4 5 6 7 8 9 10  Groups: php.internals 
Request: Send a blank email to internals+get-59917@lists.php.net to get a copy of this message
I too am on the fence, but lean more towards not liking it. Here's why:
I'm on the fence about "use ((expression) as $foo)" -- I fully like the idea of aliasing closure variables, but still thinking on the expression syntax. It would be _inconsistent_ with how it works with namespaces (which uses literals only), and _semi-consistent_ with traits (which have the "insteadof" syntax). I can definitely think of some nice use cases surrounding them, though.
I don't think I'm too fond of the analogy to the namespace "use" statement b/c that is more akin to type names, whereas in this "use" case, we're talking about how variables are scoped. For namespaces, "use" really means "alias" as opposed to "import" which I feel the name implies. What's proposed is really more closely related to the functionality of "global" since you're taking a non-local variable and making it accessible. This would be the same as this improvement: function foo() {
    global $counter as $c;
} Really though, all we're doing is, this: $foo = 5; $baz = function () use ($foo as $bar) { } Is the shorthand version of this: $foo = 5; $bar = $foo; $baz = function () use ($bar) {
    // use $bar somehow
} And with that example, I see this making less sense. The thing is that use () is a declaration time binding, which means the name is closely related to the scope its defined within (I guess it could be an include $fileReturnsFunction and that would work too). So, if that was a bad variable name 2 lines earlier, why not just change the variable name for both scopes? -ralph

« previous php.internals (#59917) next »