Repository navigation
[RFC] Typed Operator Methods (Operable, OperandSide, InvalidOperatorError) - #25670
Draft
ahmed-nour-dev wants to merge 4 commits into
Draft
ahmed-nour-dev wants to merge 4 commits into
ahmed-nour-dev wants to merge 4 commits into
Conversation
ahmed-nour-dev
force-pushed
the
rfc-typed-operator-methods
branch
2 times, most recently
from
November 23, 2026 14:11
3feb7d9 to
9414c96
Compare
ahmed-nour-dev
force-pushed
the
rfc-typed-operator-methods
branch
2 times, most recently
from
November 24, 2026 08:12
7262ff8 to
2cf6e96
Compare
Register the global built-in symbols for typed operator methods: - Add Operable interface and OperandSide enum (Left, Right) to Zend/zend_interfaces.stub.php and expose zend_ce_operable, zend_ce_operand_side, and zend_enum_OperandSide. - Add InvalidOperatorError extending TypeError to Zend/zend_exceptions.stub.php and expose zend_ce_invalid_operator_error. - Regenerate Zend/zend_interfaces_arginfo.h and Zend/zend_exceptions_arginfo.h. - Preserve default class registration order in Zend/zend_default_classes.c by registering OperandSide via zend_register_operand_side_ce() after zend_register_enum_ce(). - Add Zend/tests/operable/symbols.phpt.
Enforce compile/link-time rules for classes, interfaces, and enums implementing Operable in Zend/zend_inheritance.c: - Reject enums implementing Operable and require non-interface Operable classes to be declared readonly. - Validate __add, __sub, __mul, __div, __mod, and __pow methods (including trait-imported, early-bound subclass, and inherited non-Operable ancestor methods via zend_do_link_class() and zend_try_early_bind()). - Require public, non-static, non-variadic, by-value signatures with 1 or 2 required parameters, supported operand types, exact global OperandSide for the optional second parameter, and a non-void/never/mixed return type (no return by reference). - Require concrete operator methods to be explicitly declared final unless the class itself is final (abstract and interface methods are exempt), running the check after signature validation and reporting the declaring class name for inherited non-final methods. - Add compile/link validation tests under Zend/tests/operable/.
…or Operable Dispatch arithmetic binary operators (+, -, *, /, %, **) to Operable methods in Zend/zend_operators.c and update OPcache/JIT type inference: - Check left operand first, then right operand with OperandSide::Right, then commutative 1-parameter fallback on the right operand for + and *. - Strictly match operand types against int, float (with int widening), class/interface names (including self/parent without autoloading), unions, intersections, and DNF types using CE-cache / bounds-safe stack-buffered class resolution and zend_enum_get_case_by_id() for OperandSide cases. - Safely handle compound assignment aliasing (result == op1 || result == op2) and exception propagation without leaking or clobbering operands. - Scope zend_binop_error() to throw InvalidOperatorError only when an arithmetic operand implements Operable. - Update binary_op_result_type() in Zend/Optimizer/zend_inference.c so arithmetic operations involving MAY_BE_OBJECT allow any non-void/never return type under OPcache/JIT. - Add runtime dispatch, compound assignment, type matching, error, unary +/-, long class name, BCMath/GMP interop, and JIT hot loop tests.
Expose operator method reflection APIs in ext/reflection: - Add ReflectionMethod::isOperatorMethod(): bool using intern->ce so inherited operator methods report accurately on Operable subclasses and non-Operable bases, with case-insensitive matching for __add, __sub, __mul, __div, __mod, and __pow. - Add ReflectionClass::getOperatorMethods(): array returning an associative array of operator symbol => ReflectionMethod in deterministic order (+, -, *, /, %, **). - Regenerate ext/reflection/php_reflection_arginfo.h and ext/reflection/php_reflection_decl.h. - Add Reflection, non-Operable parent inheritance, and anonymous class_alias tests, and update ext/reflection/tests/ReflectionClass_toString_001.phpt.
ahmed-nour-dev
force-pushed
the
rfc-typed-operator-methods
branch
from
November 24, 2026 10:21
2cf6e96 to
28171f3
Compare
Author
|
Reorganized the branch into 4 clean commits (
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft implementation for the Typed Operator Methods RFC targeting PHP 8.7.
Summary
1. Built-in Symbols (
92cbd775ddd)interface Operableand unitenum OperandSide(OperandSide::Left,OperandSide::Right) toZend/zend_interfaces.stub.phpand exposezend_ce_operable/zend_ce_operand_side/zend_enum_OperandSide.class InvalidOperatorError extends TypeErrortoZend/zend_exceptions.stub.phpand exposezend_ce_invalid_operator_error.zend_register_default_classes()by registeringOperandSideviazend_register_operand_side_ce()afterzend_register_enum_ce().2. Compile/Link-Time Validation (
d9d11c32bb3)Operableclasses, interfaces, and enums inZend/zend_inheritance.c(bothzend_do_link_class()andzend_try_early_bind()):Operable.Operableclasses to be declaredreadonly.__add,__sub,__mul,__div,__mod, and__pow(including trait-imported, early-bound subclass, and inherited non-Operableancestor methods): must bepublic, non-static, non-variadic, by-value, 1 or 2 required parameters with no defaults, supported operand types (int,float, class/interface, unions, intersections, DNF — no nullability or unsupported primitives/iterable), exact globalOperandSidefor the optional 2nd parameter, and a required return type other thanvoid,never, ormixed(no return by reference).finalenforcement (no implicitfn_flagsmutation): every concrete operator method on anOperableclass must be declaredfinalunless the class itself isfinal(abstract and interface methods are exempt; non-Operableancestor methods must already befinaland report the declaring class name; anonymous classes are not exempt because they can be extended viaclass_alias()).3. Runtime Dispatch,
InvalidOperatorError, and Optimizer/JIT (32d809f45dc)+,-,*,/,%,**), unary+/-(via__mul(1)/__mul(-1)), and compound assignments (+=,-=,*=,/=,%=,**=) inZend/zend_operators.c:Operable)OperablewithOperandSide::Right)+and*onlyintwidening tofloat, resolvingself/parent, and using CE-cache + bounds-safe stack-bufferedZEND_FETCH_CLASS_NO_AUTOLOADlookups so type checks never trigger autoloaders), pluszend_enum_get_case_by_id()forOperandSidecases.result == op1 || result == op2and clean exception propagation.zend_binop_error()to throwInvalidOperatorError(subclass ofTypeError) only when an arithmetic operand implementsOperable, preserving plainTypeErrorfor non-Operableoperands and bitwise/shift operators (zero BC break for existing code).binary_op_result_type()inZend/Optimizer/zend_inference.cso arithmetic operations involvingMAY_BE_OBJECTallow any non-void/neverreturn type under OPcache/JIT.4. Reflection Support (
28171f3b3a0)ReflectionMethod::isOperatorMethod(): boolusingintern->ce(case-insensitive matching for__add..__powon the reflectedOperableclass/interface, so methods inherited from non-Operableparents reporttruevia theOperablechild andfalsevia the non-Operablebase).ReflectionClass::getOperatorMethods(): arrayreturningarray<string, ReflectionMethod>keyed by operator symbol in deterministic order (+,-,*,/,%,**).5. Test Coverage
.phpttests underZend/tests/operable/covering built-in symbols, compile/link validation, explicitfinalrules (including non-Operableparent hierarchies and anonymousclass_alias()cases), runtime dispatch, unary+/-, type matching, long class names, compound assignments, error handling, Reflection, OPcache/JIT hot loops, andBcMath\Number/GMPinterop.ext/reflection/tests/ReflectionClass_toString_001.phptforReflectionClass::getOperatorMethods().Verification
Zend/tests/operable/: 51/51 passed (1 skipped:gmp).ext/reflection/tests/: 568/568 passed (15 skipped).