Checking that Closure is static

From: Date: Fri, 18 Jun 2021 14:35:48 +0000
Subject: Checking that Closure is static
Groups: php.internals 
Request: Send a blank email to internals+get-114955@lists.php.net to get a copy of this message
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


Thread (4 messages)

« previous php.internals (#114955) next »