On Mon, March 19, 2007 2:33 pm, Wez Furlong wrote:
> I've been thinking about this on and off today too.
> Something along the lines of the following is more in the PHP spirit:
>
> $ver = phpversion();
> $fancyVer = function () { lexical $ver; return "PHP $ver"; };
>
> Where "lexical" is a keyword that means "inherit this variable from
> the current lexical scope".  I'm not suggesting that this is a good
> name for the keyword, it's just something that springs to mind.

I believe "Environment" is what it was called back in CLOS when we
schlepped it around as an extra extra argument to functions that
needed data from outside function scope...

> So, given some way to explicitly reference the scope where the
> function was "defined", what happens when you call $fancyVer after
> that scope has gone away:
>
> function doSomething() {
>    $ver = phpversion();
>    return function () { lexical $ver; return "PHP $ver"; };
> }
> $func = doSomething();
> $func(); # the doSomething() scope (hash table) doesn't exist any more
>
> This could perhaps be solved by taking a reference to $ver when the
> function is bound, but I don't know enough about the ZE to understand
> the implications of that; it would probably require a bit more state
> tracking per zend_function so that we know that we need to do that
> step during binding.

This gets incredibly complex not only to figure out what to do in the
PHP source, but for scripters to figure out what the heck it does...

If it's making YOUR head spin, what will it do to the poor unwashed
masses?...

Obviously, an anonymous function is going to be inherently complex --
but keep it as simple as it can be.

I suspect that you could get away with JUST using global and/or static
as they exist now, not introduce yet another scoping keyword, and
anything that *NEEDS* to be done with an anonymous function can be
done.

Does anybody really *NEED* a variable whose scope is non-global and
captured at the time of the func definition, carried over beyond the
scope of that until it's executed?...

Somehow I think you're just complicating it "because you can" rather
than because anybody really NEEDS this.

You have to have a pretty esoteric function to need that kind of scope
control, no?

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to