[php-src] PR #22716: Serialize closures declared in constant expressions
| From: | nicolas-grekas | Date: | Mon, 13 Jul 2026 15:47:34 +0000 |
| Subject: | [php-src] PR #22716: Serialize closures declared in constant expressions | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-37022@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22716
Author: nicolas-grekas
RFC: https://wiki.php.net/rfc/serializable_closures
Closures declared in constant expressions of a class member (anonymous closures and first-class
callables, of any visibility) become serializable as references to their declaration site: class
name, deterministic per-class id, start line.
unserialize() resolves the reference
against the loaded class; payloads contain no code and can only designate closures that classes
declare. Runtime-created closures keep refusing to serialize with the current error.
Adds Closure::fromConstExpr() and
ReflectionFunction::getConstExprId()/getConstExprClass() for exporters.
The reference is an opaque "<site>@<rank>" string naming the declaring element
(class, constant, property, property hook, or method, with parameters folded into their method) and
the closure's rank within that element, so it stays valid when unrelated elements of the class
change. First-class callable references are instead keyed by callable name
("<site>@Called::method" / "<site>@function"), since the name is
already the address.
The __serialize() payload is [<object properties>, [<tag>,
<reference>]] (the shape ext/uri and ext/random use), where the tag names the reference
kind; only "const-expr" exists today. The anonymous-closure staleness line is stored
relative to the declaring class, so edits above the class don't invalidate the reference.