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.
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
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
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!"
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
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
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
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