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 rebind scope of closure created from method

Description

When a closure is build from a method and an existing object, it is not possible to rebind it to another object.

Example

<?php

class X {
    function method () {}
}

class Y {}

$x = new X;
$fn = $x->method(...);
$ret = $fn->bindTo($x, Y::class);

?>

Alternatives

  • To be rebound, the closure must be build from within the class.

In more recent PHP versions, this error message is now :ref:cannot-rebind-scope-of-closure-created-from-method,-this-will-be-an-error-in-php-9.