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

- Thread: https://externals.io/message/59791
- Original archive: https://news-web.php.net/php.internals/59791
- Started by Laruence on 2012-04-12T15:38:21+00:00
- Messages: 13
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #59791 — Laruence — 2012-04-12T15:38:21+00:00

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

Hi:
     I have made a RFC to allow user use `T_AS` in the closure declaration, like:

     function () use($long as $l, &$long as $r) {
     }

     here is the RFC: https://wiki.php.net/rfc/useas

     any ideas?  thanks

-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #59799 — Matthew Weier O'Phinney — 2012-04-12T17:37:44+00:00

URL: https://externals.io/message/59791#59799 · In reply to: #59791

> I have made a RFC to allow user use `T_AS` in the closure declaration, like:
>
> function () use($long as $l, &$long as $r) {
> }
>
> here is the RFC: https://wiki.php.net/rfc/useas
>
> any ideas?  thanks

I really like this, as it brings a symmetry to how "use" is used when
importing classes/namespaces; I've often attempted to use "as" within my
closure use statements, only to get burned.

-- 
Matthew Weier O'Phinney
Project Lead            | matthew@zend.com
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

---

## #59858 — Verbitsky Alexander — 2012-04-13T08:10:31+00:00

URL: https://externals.io/message/59791#59858 · In reply to: #59791

> Hi:
>       I have made a RFC to allow user use `T_AS` in the closure declaration, like:
>
>       function () use($long as $l,&$long as $r) {
>       }
>
>       here is the RFC: https://wiki.php.net/rfc/useas
>
>       any ideas?  thanks
>
Closure is a functional literal that can pull with itself all variables 
visible in current scope where it was created. For my opinion using 
keyword 'use' is not necessary and i would have removed it. If you need 
different name of variable maybe you need anonymous function.

---

## #59859 — Laruence — 2012-04-13T08:14:49+00:00

URL: https://externals.io/message/59791#59859 · In reply to: #59858

On Fri, Apr 13, 2012 at 4:10 PM, Verbitsky Alexander
<verbitsky_alexandr@mail.by> wrote:
> 
> > […]
> Closure is a functional literal that can pull with itself all variables
> visible in current scope where it was created.
Hi:

   you mean dup a EG(active_symbol_table)?   it's too expensive, IMO.

thanks
> For my opinion using keyword
> 'use' is not necessary and i would have removed it. If you need different
> name of variable maybe you need anonymous function.
>
>
> --
> 
> 
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #59860 — Yasuo Ohgaki — 2012-04-13T08:19:27+00:00

URL: https://externals.io/message/59791#59860 · In reply to: #59858

Hi,

2012/4/13 Verbitsky Alexander <verbitsky_alexandr@mail.by>:
> 
> > […]
> Closure is a functional literal that can pull with itself all variables
> visible in current scope where it was created. For my opinion using keyword
> 'use' is not necessary and i would have removed it. If you need different
> name of variable maybe you need anonymous function.
>

You might be used to other language's scoping, but
this is the way PHP works.

Named function cannot do this, but anyway
function FUNC($a, $b, $c) uses ($d, $e, $f) {}
is like
function FUNC($a, $b, $c) { global $d, $e, $f; }

I would rather have this.
function FUNC($a, $b, $c) uses ($d, $e, $f) {}
Then it would be consistent with anonymous function.

There are methods, so we should be careful though.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59861 — Charlie Somerville — 2012-04-13T08:29:18+00:00

URL: https://externals.io/message/59791#59861 · In reply to: #59860

I'm at a bit of a loss as to why Laruence is claiming that allowing
closures to implicitly access variables in the outer scope requires
duplicating the symbol table.

Is there any technical reason why it's not possible for scopes to retain a
pointer to their parent scopes so variables can be looked up that way?


> Hi,
>
> 2012/4/13 Verbitsky Alexander <verbitsky_alexandr@mail.by>:
> > […]
> declaration,
> > […]
> keyword
> > […]
>
> You might be used to other language's scoping, but
> this is the way PHP works.
>
> Named function cannot do this, but anyway
> function FUNC($a, $b, $c) uses ($d, $e, $f) {}
> is like
> function FUNC($a, $b, $c) { global $d, $e, $f; }
>
> I would rather have this.
> function FUNC($a, $b, $c) uses ($d, $e, $f) {}
> Then it would be consistent with anonymous function.
>
> There are methods, so we should be careful though.
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #59862 — Yasuo Ohgaki — 2012-04-13T08:30:29+00:00

URL: https://externals.io/message/59791#59862 · In reply to: #59860

2012/4/13 Yasuo Ohgaki <yohgaki@ohgaki.net>:
>
> I would rather have this.
> function FUNC($a, $b, $c) uses ($d, $e, $f) {}
> Then it would be consistent with anonymous function.

Oops.
It became consistent, but there would be no use
with PHP. As it cannot pollute global vars.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59863 — Stas Malyshev — 2012-04-13T08:36:37+00:00

URL: https://externals.io/message/59791#59863 · In reply to: #59861

Hi!

> I'm at a bit of a loss as to why Laruence is claiming that allowing
> closures to implicitly access variables in the outer scope requires
> duplicating the symbol table.

Because variables need to be stored somewhere after the parent function
exits.

> Is there any technical reason why it's not possible for scopes to retain a
> pointer to their parent scopes so variables can be looked up that way?

Because the parent scope will be gone by the time closure is called.
Unless we retain a copy of it - which in most cases is very expensive
and impractical - you usually don't need all variables from parent scope
- you need 1-2 of them, keeping all of them linked to the closure - and
thus not freed until closure ceases to exist - would be very expensive.
Declaring shared variables explicitly is a trade-off allowing you to not
keep all the variables in case you do not need them.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #59864 — Laruence — 2012-04-13T09:21:14+00:00

URL: https://externals.io/message/59791#59864 · In reply to: #59860

> Hi,
>
> 2012/4/13 Verbitsky Alexander <verbitsky_alexandr@mail.by>:
> > […]
>
> You might be used to other language's scoping, but
> this is the way PHP works.
closure was not the way PHP works.  but now,  it has be introduced in PHP
>
> Named function cannot do this, but anyway
> function FUNC($a, $b, $c) uses ($d, $e, $f) {}
> is like
> function FUNC($a, $b, $c) { global $d, $e, $f; }

they do are different,  previous is a lexical variable.  that means it
equal to the value when the closure is created.

the latter is the value when it was called.

thanks
>
> I would rather have this.
> function FUNC($a, $b, $c) uses ($d, $e, $f) {}
> Then it would be consistent with anonymous function.
>
> There are methods, so we should be careful though.
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>
> --
> 
> 
>



-- 
Laruence  Xinchen Hui
http://www.laruence.com/

---

## #59869 — Verbitsky Alexander — 2012-04-13T09:57:36+00:00

URL: https://externals.io/message/59791#59869 · In reply to: #59863

> Hi!
>
> > […]
> Because variables need to be stored somewhere after the parent function
> exits.
>
> > […]
> Because the parent scope will be gone by the time closure is called.
> Unless we retain a copy of it - which in most cases is very expensive
> and impractical - you usually don't need all variables from parent scope
> - you need 1-2 of them, keeping all of them linked to the closure - and
> thus not freed until closure ceases to exist - would be very expensive.
> Declaring shared variables explicitly is a trade-off allowing you to not
> keep all the variables in case you do not need them.

I am not sure but it is too expensive only for memory. I don't think 
that current scope will be very big and operation for copying it very 
slowing because we work with memory (correct me if i not right). Often 
we don't to know what variables will be needed in future. And seems to 
me that can to detect which variables bound in closure and copy only it. 
Many people use PHP not because it very fast and because it very simple 
to use.  Syntax must be more clear for support. And using additional 
keyword for a bound with variables it's not doing it.

At now this is there should not alter of course. What really needed is 
'named parameters'

For example

function some_action($var1 = 0, $var2 = 0) ...

If need pass only second parameter i need to do following

some_action(0, $b) ...

Instead of

some_action($var2 = $b) ...

---

## #59872 — Yasuo Ohgaki — 2012-04-13T10:36:02+00:00

URL: https://externals.io/message/59791#59872 · In reply to: #59864

Hi,

2012/4/13 Laruence <laruence@php.net>:
> 
> > […]
> closure was not the way PHP works.  but now,  it has be introduced in PHP
> > […]
>
> they do are different,  previous is a lexical variable.  that means it
> equal to the value when the closure is created.
>
> the latter is the value when it was called.

Thanks for correction.
I should have mentioned that.

It's only similar that these are importing outside vars
into current scope. Importing is the same, but scope
is not.

I wander if there are any good named function usage
that imports current scope vars.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #59884 — Stas Malyshev — 2012-04-13T15:49:50+00:00

URL: https://externals.io/message/59791#59884 · In reply to: #59869

Hi!

> I am not sure but it is too expensive only for memory. I don't think 
> that current scope will be very big and operation for copying it very 

That depends on the scope, it can be very big - e.g. global scope. But
more important is not that it is big by itself, but that it retains
variables that are otherwise to be freed and thus increases overall
memory usage.

> we don't to know what variables will be needed in future. And seems to 
> me that can to detect which variables bound in closure and copy only it. 

If you know any way to do it, please tell. Please account for $$var, etc.

> Many people use PHP not because it very fast and because it very simple 
> to use.  Syntax must be more clear for support. And using additional 
> keyword for a bound with variables it's not doing it.

I don't think it's very complex syntax - you just specify the variables.
It takes 1 minute to read the manual and you know how to do it forever.
It's not the rocket science to write use($a, $b).

> At now this is there should not alter of course. What really needed is 
> 'named parameters'

That's entirely different topic.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #59897 — Christopher Jones — 2012-04-13T18:42:00+00:00

URL: https://externals.io/message/59791#59897 · In reply to: #59884

> > […]
>
> That's entirely different topic.
>

https://wiki.php.net/rfc/namedparameters

-- 
Email: christopher.jones@oracle.com
Tel:  +1 650 506 8630
Blog:  http://blogs.oracle.com/opal/
