# [PATCH] Allow use($var..) statement ubiquitously

- Thread: https://externals.io/message/39071
- Original archive: https://news-web.php.net/php.internals/39071
- Started by Moriyoshi Koizumi on 2008-07-17T23:23:31+00:00
- Messages: 8
- 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

---

## #39071 — Moriyoshi Koizumi — 2008-07-17T23:23:31+00:00

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

Hi,

Attached are the patches that allow the "use" statement that was  
introduced with closures to appear within every function statement  
except method definitions. I think this feature is a good addition  
because it resolves inconsistency between closures and unticked  
functions. In a nutshell,

<?php
function foo() use (&$some_globals) {
     echo $some_globals;
}
?>

is equivalent to

<?php
function foo() {
     global $some_globals;
     echo $some_globals;
}
?>

While,

<?php
function bar() {
     $some_local_var = '';
     function fubar() use (&$some_local_var) {
         echo $some_local_var;
     }
}
?>

and

<?php
function bar() {
     function fubar() {
         global $some_local_var;
         echo $some_local_var;
     }
}
?>

are of course not the same.

---

## #39074 — Larry Garfield — 2008-07-18T06:10:38+00:00

URL: https://externals.io/message/39071#39074 · In reply to: #39071

On Thursday 17 July 2008 6:23:31 pm Moriyoshi Koizumi wrote:
> Hi,
>
> Attached are the patches that allow the "use" statement that was
> introduced with closures to appear within every function statement
> except method definitions. I think this feature is a good addition
> because it resolves inconsistency between closures and unticked
> functions. In a nutshell,
>
> <?php
> function foo() use (&$some_globals) {
>      echo $some_globals;
> }
> ?>
>
> is equivalent to
>
> <?php
> function foo() {
>      global $some_globals;
>      echo $some_globals;
> }
> ?>
>
> While,
>
> <?php
> function bar() {
>      $some_local_var = '';
>      function fubar() use (&$some_local_var) {
>          echo $some_local_var;
>      }
> }
> ?>
>
> and
>
> <?php
> function bar() {
>      function fubar() {
>          global $some_local_var;
>          echo $some_local_var;
>      }
> }
> ?>
>
> are of course not the same.

Which is why I am not a fan of this syntax as proposed.  You're using the same 
keyword to mean two different but very very close things.  That's very 
confusing.  It also means that you cannot take a global by value in a 
closure.

Earlier the following was proposed:

function foo($a, &$b) global ($c, &$d) {

}

$bar = new function($a, &$b) use ($c, &$d) global ($e, &$f) {

};

Which I think is much more self-explanatory, accomplishes the same goal, and 
still does not introduce any new keywords.  (I still think "lexical" is 
better than "use", and safe, but whatev. <g>)

-- 
Larry Garfield
larry@garfieldtech.com

---

## #39078 — Moriyoshi Koizumi — 2008-07-18T07:54:17+00:00

URL: https://externals.io/message/39071#39078 · In reply to: #39074

That's one of the motivations for the patch. I never liked the new  
syntax, but if it was given a go, it should also be made consistent  
with the another part of the syntax. Oh, I just got one important  
thing in mind to mention;

test1.php:
<?php
function a() {
     $a = "bar";
     include("test2.php");
}

$a = "foo";
a();
b();
?>

test2.php:
<?php
function b() use ($a) {
     echo $a, "\n";
}
b();
?>

running test1.php ends up with two lines of "bar", surprisingly. This  
is somewhat confusing, but surely one of the things that could not  
ever be done. This might be a great help when you use a PHP-script  
file as a mark-up template.

Moriyoshi



>
> Which is why I am not a fan of this syntax as proposed.  You're  
> using the same
> keyword to mean two different but very very close things.  That's very
> confusing.  It also means that you cannot take a global by value in a
> closure.
>
> Earlier the following was proposed:
>
> function foo($a, &$b) global ($c, &$d) {
>
> }
>
> $bar = new function($a, &$b) use ($c, &$d) global ($e, &$f) {
>
> };
>
> Which I think is much more self-explanatory, accomplishes the same  
> goal, and
> still does not introduce any new keywords.  (I still think  
> "lexical" is
> better than "use", and safe, but whatev. <g>)
>
> -- 
> Larry Garfield
> larry@garfieldtech.com

---

## #39080 — Richard Quadling — 2008-07-18T10:06:10+00:00

URL: https://externals.io/message/39071#39080 · In reply to: #39078

2008/7/18 Moriyoshi Koizumi <moriyoshi@at.wakwak.com>:

> That's one of the motivations for the patch. I never liked the new syntax,
> but if it was given a go, it should also be made consistent with the another
> part of the syntax. Oh, I just got one important thing in mind to mention;
>
> test1.php:
> <?php
> function a() {
>    $a = "bar";
>    include("test2.php");
> }
>
> $a = "foo";
> a();
> b();
> ?>
>
> test2.php:
> <?php
> function b() use ($a) {
>    echo $a, "\n";
> }
> b();
> ?>
>
> running test1.php ends up with two lines of "bar", surprisingly. This is
> somewhat confusing, but surely one of the things that could not ever be
> done. This might be a great help when you use a PHP-script file as a mark-up
> template.
>
> Moriyoshi
>

It was my understanding that include-d functions were added to the global
scope (or I suppose the active namespace).

So, in that context, function b() use($a) {} should be getting the $a from
the global scope where $a == "foo".

I would say that getting 2 "bar"s is wrong.

Richard.



-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

---

## #39081 — Moriyoshi Koizumi — 2008-07-18T10:09:53+00:00

URL: https://externals.io/message/39071#39081 · In reply to: #39080

> 2008/7/18 Moriyoshi Koizumi <moriyoshi@at.wakwak.com>:
>
> running test1.php ends up with two lines of "bar", surprisingly.  
> This is somewhat confusing, but surely one of the things that could  
> not ever be done. This might be a great help when you use a PHP- 
> script file as a mark-up template.
>
> Moriyoshi
>
> It was my understanding that include-d functions were added to the  
> global scope (or I suppose the active namespace).
>
> So, in that context, function b() use($a) {} should be getting the  
> $a from the global scope where $a == "foo".
>
> I would say that getting 2 "bar"s is wrong.

Lexical scopes are completely irrelevant to which namespace the  
function belongs to.

Moriyoshi

---

## #39150 — Lukas Kahwe Smith — 2008-07-21T21:01:00+00:00

URL: https://externals.io/message/39071#39150 · In reply to: #39071

> Hi,
>
> Attached are the patches that allow the "use" statement that was  
> introduced with closures to appear within every function statement  
> except method definitions. I think this feature is a good addition  
> because it resolves inconsistency between closures and unticked  
> functions. In a nutshell,


I talked to Johannes, given that the thread has not garned much  
attention (maybe with this reply this will change) and the fact that  
since quite some time we have instead tried to encourage people to  
leverage $GLOBALS instead, it does not seem like we need to squeeze  
this new syntax into 5.3 so shortly before the feature freeze (I guess  
you get what you deserve by allowing closures in so late before the  
feature freeze?).

If we get feedback that end users that are new to PHP feel like they  
expect this syntax when they are exposed to closures we might want to  
add this in 5.4/6.0?

regards,
Lukas Kahwe Smith
mls@pooteeweet.org

---

## #39235 — Moriyoshi Koizumi — 2008-07-23T16:21:35+00:00

URL: https://externals.io/message/39071#39235 · In reply to: #39071

I would like to keep this as a RFC page in wiki.php.net. Are there  
any conventions or rules that I should keep in mind? (or just-not- 
supposed-to-do-that-because-your-proposal-is-stupid-and-will-never-be- 
accepted?)

Moriyoshi



> Hi,
>
> Attached are the patches that allow the "use" statement that was  
> introduced with closures to appear within every function statement  
> except method definitions. I think this feature is a good addition  
> because it resolves inconsistency between closures and unticked  
> functions. In a nutshell,
>
> <?php
> function foo() use (&$some_globals) {
>     echo $some_globals;
> }
> ?>
>
> is equivalent to
>
> <?php
> function foo() {
>     global $some_globals;
>     echo $some_globals;
> }
> ?>
>
> While,
>
> <?php
> function bar() {
>     $some_local_var = '';
>     function fubar() use (&$some_local_var) {
>         echo $some_local_var;
>     }
> }
> ?>
>
> and
>
> <?php
> function bar() {
>     function fubar() {
>         global $some_local_var;
>         echo $some_local_var;
>     }
> }
> ?>
>
> are of course not the same.<php-ubiquitous-use-statement- 
> HEAD-20080718.patch.diff.txt><php-ubiquitous-use-statement- 
> PHP_5.3-20080718.patch.diff.txt>
> Moriyoshi

---

## #39246 — Lukas Kahwe Smith — 2008-07-23T20:32:46+00:00

URL: https://externals.io/message/39071#39246 · In reply to: #39235

> I would like to keep this as a RFC page in wiki.php.net. Are there  
> any conventions or rules that I should keep in mind? (or just-not- 
> supposed-to-do-that-because-your-proposal-is-stupid-and-will-never- 
> be-accepted?)


we have not really done any rules .. just common sense. if you create  
a new page in the rfc namespace, it already comes up with a template  
(though that template could use some tweaking).

regards,
Lukas Kahwe Smith
mls@pooteeweet.org
