Skip to content

Fails to serialize closure if carbon is in $this #109

Open
@NiroDeveloper

Description

@NiroDeveloper

Serializable Closure Version

2.0.3

PHP Version

8.3.16

Description

It is not possible to serialize a closure that uses $this and the instance contains a Carbon variable.
object of type illuminate\support\carbon (inheriting datetime) has not been correctly initialized by calling parent::__construct()

(Tested with latest nesbot/carbon 3.8.5 and laravel 11.42.1)

Note: Seems like #55 fixes this problem if the Carbon variable is in the function (use-scope), but not in the instance.

Steps To Reproduce

class MyClass {

    // This variable is not able to be serialized with Native, because it must be initialized via the constructor.
    public Carbon $carbon;

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

    public function handle() {
        serialize(new Native(fn() => var_dump($this)));
    }

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions