Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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();

?>

Alternatives

  • Remove the static option on the closure.
  • Remove the call to bind().

In more recent PHP versions, this error message is now :ref:cannot-bind-an-instance-to-a-static-closure,-this-will-be-an-error-in-php-9.