closure icon indicating copy to clipboard operation
closure copied to clipboard

Closure::fromCallable results in Call to a member function bindTo() on null

Open fcastilloes opened this issue 3 years ago • 1 comments

Creating a closure from a callable before serialization causes an error when it is later unserialized. Both the callable and closure work apparently the same way, but there must be something different.

I got to this test script:

<?php

declare(strict_types=1);

use Opis\Closure\SerializableClosure;

use function Opis\Closure\{serialize as s, unserialize as u};

require __DIR__ . '/vendor/autoload.php';

function test() {
    echo "Hello\n";
}

// These three are just working correctly
'test'();
u(s('test'))();
unserialize(serialize('test'))();

// If explicitly creating a closure, the closure works, but the serialization doesn't
$closure = Closure::fromCallable('test');
$closure();
u(s($closure))();
unserialize(serialize(new SerializableClosure($closure)))();

When using Closure::fromCallable and then the serialization I got this error:

PHP Fatal error:  Uncaught Error: Call to a member function bindTo() on null in vendor/opis/closure/src/SerializableClosure.php:263
Stack trace:
#0 [internal function]: Opis\Closure\SerializableClosure->unserialize()
#1 vendor/opis/closure/functions.php(36): unserialize()
#2 test3.php(23): Opis\Closure\unserialize()
#3 {main}
  thrown in vendor/opis/closure/src/SerializableClosure.php on line 263

I tried to find an example of using fromCallable in the documentation but I couldn't find one. Is it something I'm missing?

fcastilloes avatar Feb 28 '22 18:02 fcastilloes

This is probably a bug

sorinsarca avatar Mar 10 '22 08:03 sorinsarca