Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP says a Closure cannot be serialized

ErrorException: Serialization of 'Closure' is not allowed

Short answer

PHP cannot turn a closure into a string, so serialize() throws as soon as a closure is inside the data. It is usually hidden in an object property that goes into a session, cache or queue. Remove the closure from what gets stored, or tell PHP to skip it with __serialize().

  • Applies to PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which object holds the closure. The stack trace names the line that called serialize(), session or cache; print the properties of the object being stored.

    php -r 'class T { public $cb; } $t = new T; $t->cb = fn() => 1; var_dump(array_map(fn($v) => is_object($v) ? get_class($v) : gettype($v), get_object_vars($t)));'
  2. 2Match what you see, then jump to its fix

    1. 1The object goes into a session, a cache or a queued job and one of its properties is a callbackAn object property holds a closureFix
    2. 2The call serialize($fn) or storing a callback array that contains a closureA closure is serialized directlyFix

What does the error look like?

  • Exception: Serialization of 'Closure' is not allowed

Cause 1 of 2

An object property holds a closure

1Check

List the properties of the stored class and look for ones assigned a function(){} or fn() value.

grep -rn "function *(\|fn *(" app/ src/ | head -30

2Fix

Tell PHP what to store. __serialize() must return an array of the values to keep, and __unserialize() restores them. Leave the closure out and rebuild it there.

public function __serialize(): array
{
    return ['name' => $this->name];
}

public function __unserialize(array $data): void
{
    $this->name = $data['name'];
    $this->callback = fn() => strtoupper($this->name);
}

3Verify

Serialize and unserialize the object in a quick script.

php -r 'require "vendor/autoload.php"; var_dump(unserialize(serialize(new MyClass())));'

Confirmed by php.net

Did this fix it?

Cause 2 of 2

A closure is serialized directly

1Check

Closure objects cannot be serialized, because they may contain bound variables and an execution context.

grep -rn "serialize(" app/ src/

2Fix

Store data that identifies the behaviour, such as a class name or a method name, and build the closure when you read it back.

// store this
$saved = serialize(['handler' => 'App\\Jobs\\Resize::run']);

// rebuild later
$data = unserialize($saved);
$handler = Closure::fromCallable($data['handler']);

3Verify

The serialize() call returns a string without an exception.

php -r 'echo serialize(["handler" => "strlen"]), "\n";'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Put plain data in sessions, caches and queue payloads, not callbacks.
  • Give objects that must be stored a __serialize() method from the start.
  • Pass class names or ids to queued jobs and let the worker load the full objects.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.