[GIT-PULLS] [php-src] PR #22716: Serialize closures declared in constant expressions

[email protected] (nicolas-grekas)
Newsgroups php.git-pulls
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.