Re: Checking that Closure is static
18 june 2021 at 17:35, Dmitry Eliseev <elisdnru@gmail.com>:
>
> Hello! When I use a static anonymous function as an HTTP-action in some frameworks like:
>
> $app = new App();
> $app->get('/', static function () {
> return new Response('Hello!);
> })
>
> I get a warning "Cannot bind an instance to a static closure" in every
> $closure->bind($container) call inside of the framework.
>
> But right now I cannot prevent this warning because there is no native way to check that
> closure is static before calling the bind method. I can only suppress the warning with @ or
> set/restore_error_handler pair.
>
> So I propose to add a method like Closure::isStatic for ability of manual checking it before
> bind call:
>
> if (!$closure->isStatic()) {
> $closure->bind($object);
> }
>
> The method can contain simple statement:
>
> return func->common.fn_flags & ZEND_ACC_STATIC;
>
> It will be helpful if somebody needs to support dynamic and static callables both without
> warnings.
I sent Pull Request https://github.com/php/php-src/pull/7193 with a
simple implementation.
P.S. Sorry for PR without RFC, but I have not got any reply to my proposal yet.
Thread (4 messages)