# T_AS in closures

- Thread: https://externals.io/message/72945
- Original archive: https://news-web.php.net/php.internals/72945
- Started by Andrey Andreev on 2014-03-05T21:28:49+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #72945 — Andrey Andreev — 2014-03-05T21:28:49+00:00

URL: https://externals.io/message/72945#72945

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.

---

## #72948 — Nikita Popov — 2014-03-05T21:38:54+00:00

URL: https://externals.io/message/72945#72948 · In reply to: #72945

> 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

---

## #72950 — Andrey Andreev — 2014-03-05T22:09:36+00:00

URL: https://externals.io/message/72945#72950 · In reply to: #72948

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.


> 
> > […]
>
>
> 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

---

## #72951 — Stas Malyshev — 2014-03-05T22:26:01+00:00

URL: https://externals.io/message/72945#72951 · In reply to: #72945

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

---

## #72953 — Andrey Andreev — 2014-03-05T22:46:38+00:00

URL: https://externals.io/message/72945#72953 · In reply to: #72951

Hi,


> Hi!
>
> > […]
>
> 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.

---

## #72954 — Stas Malyshev — 2014-03-05T22:57:47+00:00

URL: https://externals.io/message/72945#72954 · In reply to: #72953

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
> with `T_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

---

## #72955 — Andrey Andreev — 2014-03-05T23:17:41+00:00

URL: https://externals.io/message/72945#72955 · In reply to: #72954

> Hi!
>
> > […]
>
> 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.

So, exclude foreach() from my argument and all of a sudden we do have
consistency.

> > […]
>
> 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.
