# [RFC] Scope functions

- Thread: https://externals.io/message/130977
- Original archive: https://news-web.php.net/php.internals/130977
- Started by MCode on 2026-05-20T21:01:51+00:00
- Messages: 1
- RFC: Scope Functions (Under discussion) — https://externals.io/rfc/scope-functions.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #130977 — MCode — 2026-05-20T21:01:51+00:00

URL: https://externals.io/message/130977#130977

> Thanks for this RFC, here are my 2cents:
 >
 > I think the current syntax is too close to current short closures, and
 > since it allows multiline closures, it would imply that by default all
 > multiline short closures are scoped, which is not intuitive since
 > traditional closures don't capture by default.
 > Instead, I think replacing the `fn () { }`  syntax to add a keyboard
 > indicating it inherits the scope would drastically help making it more
 > intuitive.
 >
 > I would imagine instead something like this:
 >
 >     $externalVar = 0;
 >     $closure1 = scoped fn () => $newVar1 = $externalVar++;
 >     $closure2 = scoped function () {
 >         return $newVar2 = $externalVar++;
 >     };
 >     $closure1();
 >     $closure2();
 >     var_dump($newVar1); // int(0)
 >     var_dump($newVar2); // int(1)
 >     var_dump($externalVar); // int(2)
 >
 > This way, for the first case, the only new information that `scoped`
 > brings is the potential existence of `$newVar1` after the closure's
 > execution, and for the second closure, it makes it explicit that all
 > variables in the closure's body are shared with the parent scope.
 >
 > WDYT?
 >
 > Le 06/05/2026 à 22:09, Bob Weinand a écrit :
> > […]

I agree that the new syntax is too close to current short closures.

I don't think we need a new keyword though, I think the presence of `=>` 
in the function definition should indicate that scope is captured, like 
this:

```
function() => {
     // ...
}
```

To put it another way `fn() => foo;` would be a syntactically identical 
version of `function() => {return foo;}`
