[php-src] PR #22635: feat: Implement extension methods (RFC prototype, phase 1)
| From: | hollyschilling | Date: | Wed, 08 Jul 2026 05:16:53 +0000 |
| Subject: | [php-src] PR #22635: feat: Implement extension methods (RFC prototype, phase 1) | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-36901@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22635
Author: hollyschilling
Adds Swift-style extension blocks that declare additional methods for an existing class or
interface, resolved at call time only when the receiver does not define the method itself:
extension Target {
public function helper(): string { /* $this is the receiver */ }
}
-
extension is a contextual keyword (T_EXTENSION), lexed only when followed by another
identifier, mirroring the enum technique; a extension extends/implements carve-out
preserves classes named "extension", and T_EXTENSION is semi-reserved.
- Extension blocks compile to a synthetic final anonymous class; a new ZEND_BIND_EXTENSION opcode
(212) registers its methods, keyed by the lowercased resolved target name, when the declaration
executes (load-gated, like function declarations).
- zend_std_get_method gains one fallback on its miss path: real method, then __call trampoline, then
extension registry lookup (most-derived target first: inheritance chain, then interfaces).
Registered methods are flagged ZEND_ACC_NEVER_CACHE so the polymorphic inline cache and JIT are
bypassed in this prototype.
- Methods only: properties, constants, and cases are compile errors.
- Indirect invocation (callables, reflection, method_exists) deliberately does not see extensions.
PROTOTYPE LIMITATIONS: the registry is a process global, so user-class method pointers dangle after
request 1 in multi-request SAPIs (CLI only until it moves into zend_executor_globals); duplicate
registrations are silently ignored (first wins).