Re: [RFC] Draft - Closure self reference
| From: | Christoph M. Becker | Date: | Wed, 11 Nov 2020 17:59:41 +0000 |
| Subject: | Re: [RFC] Draft - Closure self reference | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-112225@lists.php.net to get a copy of this message | ||
On 11.11.2020 at 18:39, Dan Ackroyd wrote:
> On Tue, 10 Nov 2020 at 17:39, Hans Henrik Bergan <divinity76@gmail.com> wrote:
>>
>> something i'm missing from Javascript is the ability to give names to
>> closures, ...the name is optional, and only visible inside the closure
>> itself, and unfortunately this is not legal in PHP, i wish it was.
>
> I really like that...but unfortunately that wouldn't work in PHP.
>
> In JS, when a function is declared inside another function, the name
> of it is limited to the scope of the containing function. In PHP, when
> a function is declared inside another function, it is put into the
> current namespace's global scope.
>
> Changing how scope works in PHP would be too large a change for just this.
In JavaScript, a named function expression is different to a function
declaration:
var fn = function foo() {console.log('blah')}
foo()
=> Uncaught ReferenceError: foo is not defined
vs.
function foo() {console.log('blah')}
foo()
=> blah
So the named function expression is still an anonymous function; the
label is only defined inside of the function body.
Christoph