Git Product home page Git Product logo

Comments (6)

nunomaduro avatar nunomaduro commented on May 25, 2024

You forgot to serialize the fn () => 'Hi' closure, and you also forgot to bind the static::class. Here is your example fixed:

class BindToMe
{
    private $closure;

    public function __construct($closure)
    {
        $this->closure = $closure;
    }

    public function hello()
    {
        echo ($this->closure)();
    }
}

$serializable = new SerializableClosure(
    Closure::bind(
        function () {
            $this->hello();
        },
        new BindToMe(new SerializableClosure(fn() => 'Hi')),
        BindToMe::class
    )
);

unserialize(serialize($serializable))->getClosure()(); // Hi

from serializable-closure.

henze-housepedia avatar henze-housepedia commented on May 25, 2024

eh... but this has worked with the opis/closure package. Besides, I have checked this Native.php file, and it uses a static:from. That should point to a method in the same object (Native) and I can't find 'from'.

Also, if I wrote that class I encountered the error in, I could fix that yes, and I can add a workaround using opis that serializes it before this package does, but the missing 'from' method is clearly a bug.

adding the missing newScope parameter has no effect.

As this is used in laravel, this either is a breaking change or a bug (missing from method in the wrapClosures method of the Native class, line 227)

from serializable-closure.

henze-housepedia avatar henze-housepedia commented on May 25, 2024

This bug would be fixed if you simply add the following code to the Native class:

public static function from(Closure $closure)
    {
        if (static::$context === null) {
            $instance = new static($closure);
        } elseif (isset(static::$context->scope[$closure])) {
            $instance = static::$context->scope[$closure];
        } else {
            $instance = new static($closure);
            static::$context->scope[$closure] = $instance;
        }

        return $instance;
    }

from serializable-closure.

nunomaduro avatar nunomaduro commented on May 25, 2024

Can you create an open source repository with that example working with the opis/closure package? I've just tested it, using opis/closure and laravel/serializable closure, and in both it requires the changes I've mentioned.

from serializable-closure.

henze-housepedia avatar henze-housepedia commented on May 25, 2024

Here you go:
https://github.com/henze-housepedia/laravel-serializable-closure-bug

You where right about the newScope, and Opis requires the class variable without type definition, but other than that, Opis works like a charm, while this package still needs the 'from' method as mentioned in an earlier comment.

I saw a new release, but that won't fix it.

from serializable-closure.

nunomaduro avatar nunomaduro commented on May 25, 2024

@henze-housepedia Thank you for replicating the bug on that open source repository.

from serializable-closure.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.