On Tue, 10 Nov 2020 at 17:39, Hans Henrik Bergan <divinit...@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.

Levi Morrison wrote:
> Is there any way we can spell it `__FUNCTION__`?

I think using some sort of constant, rather than a magic variable
sounds is probably the way to go. I would hope we could find something
better than that as:

return __FUNCTION__($n-1) + __FUNCTION__($n-2);

is pretty hard on my eyes.

I think I'll wander over to the 'Support for <func>::function syntax' thread...

cheers
Dan
Ack


# PHP

function foo1() {
    function bar() {}
}

function foo2() {
    function bar() {}
}

foo1();
foo2();

// Fatal error: Cannot redeclare bar()


# JS

function foo1() {
  var fn = function TheClosuresLocalName(){
    console.log("I am closure inside foo1");
  }
  return fn;
}

function foo2() {
  var fn = function TheClosuresLocalName(){
    console.log("I am closure inside foo2");
  }
  return fn;
}

fn1 = foo1();
fn2 = foo2();
fn1();
fn2();

"I am closure inside foo1"
"I am closure inside foo2"

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

Reply via email to