Cannot bind an instance to a static closure

Description

The Closure::bind() method gives the scope of execution of a closure. And a static closure uses no scope, so the bind() method fails.

Example

<?php
    $closure = static function () { return __METHOD__;};

    $closure->bindTo(new Stdclass());
    $closure();

?>

Solutions

  • Remove the static option on the closure.

  • Remove the call to bind().

In more recent PHP versions, this error message is now Cannot bind an instance to a static closure, this will be an error in PHP 9.