opis / closure

Serialize closures (anonymous functions)
https://opis.io/closure
MIT License
2.5k stars 84 forks source link

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

Open fcastilloes opened 2 years ago

fcastilloes commented 2 years ago

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?

sorinsarca commented 2 years ago

This is probably a bug