- Serializable Closure Version: v1.1.0
- Laravel Version: not laravel scope
- PHP Version: 8.0.15
Description:
I created closure from object method and tried to serialize it, however once unserialized it fails because of type hint used for this method.
Steps To Reproduce:
Minimal code example is below:
Entry point script:
<?php
use Laravel\SerializableClosure\SerializableClosure;
use Rela589n\PhpDecorationIssue\SerializableClosure\SerializableClosureIssue;
require 'vendor/autoload.php';
$object = new SerializableClosureIssue();
/** @var SerializableClosure $original */
$original = unserialize(
serialize(
new SerializableClosure(Closure::fromCallable([$object, 'reproduceIssue']))
)
);
var_dump($original->getClosure()());
Class with method which will be used as closure:
<?php
declare(strict_types=1);
namespace Rela589n\PhpDecorationIssue\SerializableClosure;
use JetBrains\PhpStorm\Immutable;
#[Immutable]
final class SerializableClosureIssue
{
public function reproduceIssue(): Issue
{
return new Issue();
}
}
Class for type-hinting in method:
<?php
declare(strict_types=1);
namespace Rela589n\PhpDecorationIssue\SerializableClosure;
use JetBrains\PhpStorm\Immutable;
#[Immutable]
final class Issue
{
}
The foregoing code gives next issue:
PHP Fatal error: Uncaught Error: Class "Issue" not found in laravel-serializable-closure://function (): \Issue
{
return new \Issue();
}:4
Stack trace:
#0 /home/rela589n/projects/php-decoration-issue/serializable-closure.php(17): Rela589n\PhpDecorationIssue\SerializableClosure\SerializableClosureIssue::{closure}()
#1 {main}
thrown in laravel-serializable-closure://function (): \Issue
{
return new \Issue();
} on line 4
Thanks
Thank you for this amazing package, it really complements missing PHP feature.