Nikita Nefedov wrote on 01/10/2015 12:07:
And yet we have super-globals. You can also access variables of one
script's global scope from another script's global scope without
importing or anything like that.
You are right, super-globals are an exception to the rule, because they
appear in all function scopes.
An include doesn't change the scope, so I don't think that changes the
rule at all - if you use include() while you're in a function, the
variables all still refer to the function scope, and don't automatically
import or export anything.
I wouldn't say that PHP has or follows any idiomatic "way" of how
scopes should work, it's just evolutionary.
That's not how Rasmus expressed it
[http://marc.info/?l=php-internals&m=144107616411299&w=2]:
> I made a very deliberate decision In the very first implementation of
PHP to avoid scope side-effects like this. Inside a function everything
is local unless explicitly declared not to be. This has carried through
for the past 20+ years in slightly different ways, but the basic rule
has stayed consistent even for closures.
If auto-capture is the main feature you are interested in, and syntax
just a by-product, then it might be sensible to make that clear. As a
straw man, it would be possible to have auto-capture closures with no
new syntax at all, e.g. function($x) use(*) { return $x * $y; }
I believe Levi's and Bob's intentions are not to introduce
auto-capture but to add short closures, auto-capture being a tool that
works best to help in achieving this.
It is a tool for making them shorter, yes, but it is not the only way,
and it comes with a cost of changing established behaviour. Again, look
at C++'s lambda syntax - it is much shorter than PHP's, but requires a
list of all variables from the outer scope being captured.
There are actually 3 tools being used to make it shorter in the current RFC:
- replacing "function" keyword with syntax elements, or a shorter keyword
- making the "return" keyword implicit (in one variant of the syntax,
the other having a full function body)
- eliminating the entire "use" clause by automatically capturing all
used variables
I am suggesting the first two be kept, but replace the third one with:
- shortening the "use" clause by tightening up the syntax
`use(*)` just as long as `use($y)`, and the shorter version where you
need to separate args from uses with semicolon will be harder to read
(ex.: `fn($x; $y) => $x * $y` vs `fn($x) => $x * $y` - it's not
obvious at all what semicolon does.
You've picked on one syntax I explicitly labelled as "a straw man", and
one idea which was proposed by somebody else a few hours ago, and use
those to somehow dismiss the whole idea of a short syntax which lists
captured variables. I have come up with about a dozen different ideas in
various parts of this discussion, and could list dozens more. They would
all have their pros and cons, but I don't see how you can dismiss the
whole concept, UNLESS you consider the auto-capture to be an end in its
own right, rather than "just a way of making it shorter".
Whereas with auto capture you can at least guess that this variable
will be captured from defining scope, if you don't know how this
feature works).
Well, you certainly can't guess it from the behaviour of anything else
in PHP. You could guess based on the behaviour of other languages where
scope works differently, though, I suppose.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php