PHP says a Closure cannot be serialized
Exception: Serialization of 'Closure' is not allowedShort 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().
Fix it fast
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)));'2Match what you see, then jump to its fix
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 -302Fix
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
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix