zahidgani's avatar

php artisan generate error Closure::__set_state()

[Symfony\Component\Debug\Exception\FatalThrowableError] Call to undefined method Closure::__set_state()

I have used laravel 5.4 when I try to use php artisan then it generate error.

0 likes
4 replies
pandhuweni's avatar

Delete config.php at

{root}/bootstrap/cache

this works for me (y)

3 likes
OstapBrehin's avatar

My problem was that I used class instances in custom config files.

To solve this I came up with this PHP trait that makes a class with public properties serializable. (works on read-only classes as well)

<?php

namespace App;

/**
 * Adds __set_state for proper serialization in config caching.
 *
 * Use in classes with public properties and matching constructor params.
 */
trait Serializable
{
    public static function __set_state(array $array)
    {
        return new static(...$array);
    }
}

Example

<?php

namespace App;

readonly class Plan
{
    use Serializable;

    public function __construct(
        public string       $name,
        public PlanSlugEnum $slug,
<?php
// config/myapp/plans.php

use App\{Plan, PlanSlugEnum};

return [
    new Plan(
        name             : 'Free',
        slug             : PlanSlugEnum::FREE,
        stripeId         : null,
        dataLimitGB      : 1,
        requestsPerMinute: 10,
    ),
    new Plan(
        name             : 'Starter Monthly',
        slug             : PlanSlugEnum::STARTER_MONTHLY,

Please or to participate in this conversation.