[php-src] PR #25670: [RFC] Typed Operator Methods (Operable, Side, InvalidOperatorError)
| From: | ahmed-nour-dev | Date: | Mon, 23 Nov 2026 12:11:21 +0000 |
| Subject: | [php-src] PR #25670: [RFC] Typed Operator Methods (Operable, Side, InvalidOperatorError) | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40923@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25670
Author: ahmed-nour-dev
Draft implementation for the Typed Operator Methods RFC targeting PHP 8.7.
## Summary
### 1. Built-in Symbols (
0588745565d)
- Add interface Operable and unit enum Side (Side::Left,
Side::Right) to Zend/zend_interfaces.stub.php and expose
zend_ce_operable / zend_ce_side.
- Add class InvalidOperatorError extends TypeError to
Zend/zend_exceptions.stub.php and expose zend_ce_invalid_operator_error.
- Register enum class entries before interfaces in zend_register_default_classes() so
UnitEnum and enum object handlers are initialized before Side.
### 2. Compile/Link-Time Validation (901c510e045)
- Validate Operable classes, interfaces, and enums in
Zend/zend_inheritance.c (both zend_do_link_class() and
zend_try_early_bind()):
- Reject enums implementing Operable.
- Require non-interface Operable classes to be declared readonly.
- Validate __add, __sub, __mul, __div,
__mod, and __pow (including trait-imported and inherited/early-bound
methods): must be public, non-static, non-variadic, 1 or 2 required parameters,
supported operand types (int, float, class/interface, unions,
intersections, DNF — no nullability or unsupported primitives/iterable), exact global
Side for the optional 2nd parameter, and a required return type other than
void, never, or mixed.
- Implicitly mark concrete operator methods ZEND_ACC_FINAL.
### 3. Runtime Dispatch & InvalidOperatorError (49089fedaad)
- Dispatch binary arithmetic operators (+, -, *,
/, %, **) and compound assignments (+=,
-=, *=, /=, %=, **=) in
Zend/zend_operators.c:
1. Left operand (Operable)
2. Right operand (Operable with Side::Right)
3. Commutative 1-parameter fallback on the right operand for + and *
only
- Strict operand type matching (allowing int widening to float, resolving
self/parent, and using ZEND_FETCH_CLASS_NO_AUTOLOAD so type
checks never trigger autoloaders).
- Safe aliasing handling when result == op1 || result == op2 and clean exception
propagation.
- Throw InvalidOperatorError (subclass of TypeError) for unsupported
arithmetic operand types while preserving plain TypeError for bitwise and shift
operators.
### 4. Reflection Support (17b2dfb0f28)
- Add ReflectionMethod::isOperatorMethod(): bool (case-insensitive matching for
__add..__pow on Operable classes/interfaces).
- Add ReflectionClass::getOperatorMethods(): array returning array<string,
ReflectionMethod> keyed by operator symbol in deterministic order (+,
-, *, /, %, **).
### 5. Test Coverage (bdda83a0093)
- Added 41 .phpt tests under Zend/tests/operable/ covering built-in
symbols, compile/link validation, runtime dispatch, type matching, compound assignments, error
handling, Reflection, OPcache/JIT hot loops, and BcMath\Number / GMP
interop.
- Updated 22 Zend/tests/*.phpt, 5 ext/bcmath/tests/*.phpt, and
ext/reflection/tests/ReflectionClass_toString_001.phpt.
## Verification
- Zend/tests/operable/: 40/40 passed (1 skipped: gmp), under both tracing
JIT and function JIT (--enable-debug, 0 leaks).
- Zend/tests/: 5604/5604 passed (0 failures).
- ext/reflection/tests/: 567/567 passed (0 failures).
- ext/bcmath/tests/: 170/170 passed (0 failures).