# Add ReflectionFunctionAbstract::isAnonymous()

- Thread: https://externals.io/message/116282
- Original archive: https://news-web.php.net/php.internals/116282
- Started by Dylan K. Taylor on 2021-10-20T23:12:08+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #116282 — Dylan K. Taylor — 2021-10-20T23:12:08+00:00

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

Hi all, 

Given the addition of Closure::fromCallable() and the upcoming first-class callable syntax in 8.1, it seems slightly problematic that there's no simple way to tell by reflection if a Closure refers to an anonymous function or not. ReflectionFunctionAbstract::isClosure() (perhaps somewhat misleadingly) returns whether the closure is literally a \Closure instance, so it's not useful for this purpose. 

The only way to do this currently (that I know about) is to check if the name of the function contains "{closure}", which is a bit unpleasant and depends on undocumented behaviour. 

I'm proposing the addition of ReflectionFunctionAbstract::isAnonymous(), which would fill this use case, and may be able to offer an implementation. 
 
Thanks, 
Dylan Taylor.

---

## #116283 — Nikita Popov — 2021-10-21T07:44:42+00:00

URL: https://externals.io/message/116282#116283 · In reply to: #116282

> Hi all,
>
> Given the addition of Closure::fromCallable() and the upcoming first-class
> callable syntax in 8.1, it seems slightly problematic that there's no
> simple way to tell by reflection if a Closure refers to an anonymous
> function or not. ReflectionFunctionAbstract::isClosure() (perhaps somewhat
> misleadingly) returns whether the closure is literally a \Closure instance,
> so it's not useful for this purpose.
>
> The only way to do this currently (that I know about) is to check if the
> name of the function contains "{closure}", which is a bit unpleasant and
> depends on undocumented behaviour.
>
> I'm proposing the addition of ReflectionFunctionAbstract::isAnonymous(),
> which would fill this use case, and may be able to offer an implementation.
>

Sounds reasonable to me!

Nikita

---

## #116287 — Aaron Piotrowski — 2021-10-21T21:07:26+00:00

URL: https://externals.io/message/116282#116287 · In reply to: #116282

> 
> 
> Hi all, 
> 
> Given the addition of Closure::fromCallable() and the upcoming first-class callable syntax in 8.1, it seems slightly problematic that there's no simple way to tell by reflection if a Closure refers to an anonymous function or not. ReflectionFunctionAbstract::isClosure() (perhaps somewhat misleadingly) returns whether the closure is literally a \Closure instance, so it's not useful for this purpose. 
> 
> The only way to do this currently (that I know about) is to check if the name of the function contains "{closure}", which is a bit unpleasant and depends on undocumented behaviour. 
> 
> I'm proposing the addition of ReflectionFunctionAbstract::isAnonymous(), which would fill this use case, and may be able to offer an implementation. 
> 
> Thanks, 
> Dylan Taylor. 
> 
> 
> -- 
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
> 

Hi Dylan,

I recently wrote some code checking for “{closure}”: https://github.com/amphp/amp/blob/27219ddbc0bbc3fd0db4d7380eaed6489c7291ed/lib/functions.php#L135 <https://github.com/amphp/amp/blob/27219ddbc0bbc3fd0db4d7380eaed6489c7291ed/lib/functions.php#L135>

I agree, it is a bit unpleasant and looks like a hack. I would welcome an isAnonymous() method.

Cheers,
Aaron Piotrowski

---

## #116292 — Claude Pache — 2021-10-22T13:19:23+00:00

URL: https://externals.io/message/116282#116292 · In reply to: #116282

> Le 21 oct. 2021 à 01:12, Dylan K. Taylor <dktapps@pmmp.io> a écrit :
> 
> Hi all, 
> 
> Given the addition of Closure::fromCallable() and the upcoming first-class callable syntax in 8.1, it seems slightly problematic that there's no simple way to tell by reflection if a Closure refers to an anonymous function or not. ReflectionFunctionAbstract::isClosure() (perhaps somewhat misleadingly) returns whether the closure is literally a \Closure instance, so it's not useful for this purpose. 
> 
> The only way to do this currently (that I know about) is to check if the name of the function contains "{closure}", which is a bit unpleasant and depends on undocumented behaviour. 
> 
> I'm proposing the addition of ReflectionFunctionAbstract::isAnonymous(), which would fill this use case, and may be able to offer an implementation. 
> 
> Thanks, 
> Dylan Taylor. 
> 
> 
> -- 
> 
> To unsubscribe, visit: https://www.php.net/unsub.php
> 


Hi,

Per the manual [1], Closure::fromCallable() “creates and returns a new anonymous function”. I guess that this might not match your notion of “anonymous function”?

Therefore, I am asking for clarification: What practical distinction do you make between ”an instance of Closure” and “an anonymous function”, and why does this distinction matter?

[1]: https://www.php.net/manual/en/closure.fromcallable.php <https://www.php.net/manual/en/closure.fromcallable.php>

—Claude

---

## #116293 — Dylan K. Taylor — 2021-10-22T13:41:27+00:00

URL: https://externals.io/message/116282#116293 · In reply to: #116292

---- On Fri, 22 Oct 2021 14:19:23 +0100 Claude Pache <claude.pache@gmail.com> wrote ----

 > 
 > Le 21 oct. 2021 à 01:12, Dylan K. Taylor <dktapps@pmmp.io> a écrit :
 > 
 > Hi all, 
 > 
 > Given the addition of Closure::fromCallable() and the upcoming first-class callable syntax in 8.1, it seems slightly problematic that there's no simple way to tell by reflection if a Closure refers to an anonymous function or not. ReflectionFunctionAbstract::isClosure() (perhaps somewhat misleadingly) returns whether the closure is literally a \Closure instance, so it's not useful for this purpose. 
 > 
 > The only way to do this currently (that I know about) is to check if the name of the function contains "{closure}", which is a bit unpleasant and depends on undocumented behaviour. 
 > 
 > I'm proposing the addition of ReflectionFunctionAbstract::isAnonymous(), which would fill this use case, and may be able to offer an implementation. 
 > 
 > Thanks, 
 > Dylan Taylor. 
 > 
 > 
 > -- 
 > 
 > To unsubscribe, visit: https://www.php.net/unsub.php
 > 
 > 
 > 
 > Hi,
 > 
 > Per the manual [1], Closure::fromCallable() “creates and returns a new anonymous function”. I guess that this might not match your notion of “anonymous function”?
 > 
 > Therefore, I am asking for clarification: What practical distinction do you make between ”an instance of Closure” and “an anonymous function”, and why does this distinction matter?
 > 
 > [1]: https://www.php.net/manual/en/closure.fromcallable.php
 > 
 > —Claude
 > 

Hi Claude, 
Sorry for the double email, my previous reply got bounced from the mailing list because I replied from the wrong address.

An anonymous function would be an unnamed function, e.g. arrow function or function(){}.

I guess the documentation for Closure::fromCallable() ought to be updated, because Closure::fromCallable('namedFunc') isn't really anonymous, it's a \Closure object that refers to a named function. A \Closure may refer to a named or an anonymous function, and currently there's no way to tell the difference without hacks.

The distinction is important for reflection cases, such as the one Aaron mentioned, and also like generating a pretty name for closures in a project I maintain [1].

It's a fringe use case for sure, but considering we already have ReflectionClass->isAnonymous(), I think it makes sense.

[1]: https://github.com/pmmp/PocketMine-MP/blob/986b4e0651d665c72ec011542f95b4bd9529c6a8/src/pocketmine/utils/Utils.php#L144

Dylan

---

## #117671 — Nicolas Grekas — 2022-05-05T13:59:37+00:00

URL: https://externals.io/message/116282#117671 · In reply to: #116293

Le ven. 22 oct. 2021 à 15:41, Dylan K. Taylor <dktapps@pmmp.io> a écrit :

>
> ---- On Fri, 22 Oct 2021 14:19:23 +0100 Claude Pache <
> claude.pache@gmail.com> wrote ----
>
> > […]
> first-class callable syntax in 8.1, it seems slightly problematic that
> there's no simple way to tell by reflection if a Closure refers to an
> anonymous function or not. ReflectionFunctionAbstract::isClosure() (perhaps
> somewhat misleadingly) returns whether the closure is literally a \Closure
> instance, so it's not useful for this purpose.
> > […]
> the name of the function contains "{closure}", which is a bit unpleasant
> and depends on undocumented behaviour.
> > […]
> ReflectionFunctionAbstract::isAnonymous(), which would fill this use case,
> and may be able to offer an implementation.
> > […]
> anonymous function”. I guess that this might not match your notion of
> “anonymous function”?
> > […]
> you make between ”an instance of Closure” and “an anonymous function”, and
> why does this distinction matter?
> > […]
>
> Hi Claude,
> Sorry for the double email, my previous reply got bounced from the mailing
> list because I replied from the wrong address.
>
> An anonymous function would be an unnamed function, e.g. arrow function or
> function(){}.
>
> I guess the documentation for Closure::fromCallable() ought to be updated,
> because Closure::fromCallable('namedFunc') isn't really anonymous, it's a
> \Closure object that refers to a named function. A \Closure may refer to a
> named or an anonymous function, and currently there's no way to tell the
> difference without hacks.
>
> The distinction is important for reflection cases, such as the one Aaron
> mentioned, and also like generating a pretty name for closures in a project
> I maintain [1].
>
> It's a fringe use case for sure, but considering we already have
> ReflectionClass->isAnonymous(), I think it makes sense.
>
> [1]:
> https://github.com/pmmp/PocketMine-MP/blob/986b4e0651d665c72ec011542f95b4bd9529c6a8/src/pocketmine/utils/Utils.php#L144
>
>
>

For the record, I submitted a PR adding ReflectionFunction::isAnonymous()
at
https://github.com/php/php-src/pull/8499

Cheers,
Nicolas
