> Le 18 juin 2021 à 16:35, Дмитрий Елисеев <elisd...@gmail.com> a écrit :
> 
> 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 (or
> Closure::canBeBound, etc.) 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.
> 
> -- Best, Dmitry

I think that the Warning should be removed entirely, and the `bindTo()` method 
should be a no-op when applied to a static closure.

That the closure is implemented as static, or as non-static without actually 
using `$this`, is an implementation detail that the user of the closure doesn’t 
need to be aware.

As a precedent, it is perfectly legal to call a static class method on an 
instance of that class: https://3v4l.org/5QKsN <https://3v4l.org/5QKsN> 

—Claude

Reply via email to