Hi,
I was clicking on bugs.php.net/random today and I came across #54888,
which is a feature request for the following syntax:
$foo = 'whatever';
$bar = function() use ($foo as $baz) { /* use $baz here */ };
https://bugs.php.net/bug.php?id=54888
I don't know if other languages have it, but it seems nice and
considering how much developers prefer foreach() over other loops
because it allows them to "rename" the variable, I'm sure people would
love it.
Has this been discussed before? What are your opinions on it?
Regards,
Andrey Andreev.
Hi,
I was clicking on bugs.php.net/random today and I came across #54888,
which is a feature request for the following syntax:$foo = 'whatever'; $bar = function() use ($foo as $baz) { /* use $baz here */ };
https://bugs.php.net/bug.php?id=54888
I don't know if other languages have it, but it seems nice and
considering how much developers prefer foreach() over other loops
because it allows them to "rename" the variable, I'm sure people would
love it.Has this been discussed before? What are your opinions on it?
Regards,
Andrey Andreev.
There has been an RFC for this: https://wiki.php.net/rfc/useas
IIRC the RFC was withdrawn because the reception was not very positive. You
might want to have a look at the discussion, see
http://markmail.org/message/q53myujswaesca2u and
http://markmail.org/message/nrptrylgkarqrpxk.
Nikita
From what I read (and that's all of it), people on the list were
against (expression) as $foo and wanted more usage examples in the
RFC. The overall reception seems positive to me. :)
The author withdrew it because they wanted to add more stuff to the
patch and then (apparently) never proposed it again.
Actually, that discussion also mentions use (...) with regular
functions, which would also be nice, but probably better left for
another thread.
Hi,
I was clicking on bugs.php.net/random today and I came across #54888,
which is a feature request for the following syntax:$foo = 'whatever'; $bar = function() use ($foo as $baz) { /* use $baz here */ };
https://bugs.php.net/bug.php?id=54888
I don't know if other languages have it, but it seems nice and
considering how much developers prefer foreach() over other loops
because it allows them to "rename" the variable, I'm sure people would
love it.Has this been discussed before? What are your opinions on it?
Regards,
Andrey Andreev.There has been an RFC for this: https://wiki.php.net/rfc/useas
IIRC the RFC was withdrawn because the reception was not very positive. You
might want to have a look at the discussion, see
http://markmail.org/message/q53myujswaesca2u and
http://markmail.org/message/nrptrylgkarqrpxk.Nikita
Hi!
I was clicking on bugs.php.net/random today and I came across #54888,
which is a feature request for the following syntax:$foo = 'whatever'; $bar = function() use ($foo as $baz) { /* use $baz here */ };
What this allows to do that simple $baz = $foo and use $baz wouldn't do?
I think adding syntax that does not substantially expand what you can do
is not right. It just makes language larger and harder to learn and
understand.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi,
Hi!
I was clicking on bugs.php.net/random today and I came across #54888,
which is a feature request for the following syntax:$foo = 'whatever'; $bar = function() use ($foo as $baz) { /* use $baz here */ };
What this allows to do that simple $baz = $foo and use $baz wouldn't do?
I think adding syntax that does not substantially expand what you can do
is not right. It just makes language larger and harder to learn and
understand.
To answer your question: nothing, except that $baz will only exist in
the scope of the anonymous function.
But this is not about something that can't be achieved with existing
features, it's about cleaner code and to some extent - consistency
with T_AS
in foreach(), traits, namespaces; and also how regular
functions would have local-scope names for variables that are passed
to them.
Hi!
But this is not about something that can't be achieved with existing
features, it's about cleaner code and to some extent - consistency
withT_AS
in foreach(), traits, namespaces; and also how regular
There's no issue of consistency there - foreach is not used to "rename
variables", foreach is used to iterate. If anything, that is opposite of
consistency, since "as" is used in two contradictory roles - for naming
iterating variable and for renaming variable without iterating.
I don't see how it's any "consistency" to add a new feature while
reusing syntax used in existing features for different purposes.
functions would have local-scope names for variables that are passed
to them.
Those things are called function parameters, and anonymous functions use
them the same way as named functions, no difference there either. And of
course anon functions can have local vars too.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
But this is not about something that can't be achieved with existing
features, it's about cleaner code and to some extent - consistency
withT_AS
in foreach(), traits, namespaces; and also how regularThere's no issue of consistency there - foreach is not used to "rename
variables", foreach is used to iterate. If anything, that is opposite of
consistency, since "as" is used in two contradictory roles - for naming
iterating variable and for renaming variable without iterating.
I don't see how it's any "consistency" to add a new feature while
reusing syntax used in existing features for different purposes.
So, exclude foreach() from my argument and all of a sudden we do have
consistency.
functions would have local-scope names for variables that are passed
to them.Those things are called function parameters, and anonymous functions use
them the same way as named functions, no difference there either. And of
course anon functions can have local vars too.
I didn't need education on that, but to your credit - I like how you
always manage to twist arguments to your own liking. :)
You know I meant that in the context of renaming.
Anyway, got it: -1 from you.
Cheers,
Andrey.