Skip to content

[RFC] Typed Operator Methods (Operable, OperandSide, InvalidOperatorError) - #25670

Draft
ahmed-nour-dev wants to merge 4 commits into
php:masterfrom
ahmed-nour-dev:rfc-typed-operator-methods
Draft

ahmed-nour-dev wants to merge 4 commits into
php:masterfrom
ahmed-nour-dev:rfc-typed-operator-methods

Conversation

@ahmed-nour-dev

@ahmed-nour-dev ahmed-nour-dev commented Nov 23, 2026 •

Copy link
Copy Markdown

Draft implementation for the Typed Operator Methods RFC targeting PHP 8.7.

Summary

1. Built-in Symbols (92cbd775ddd)

  • Add interface Operable and unit enum OperandSide (OperandSide::Left, OperandSide::Right) to Zend/zend_interfaces.stub.php and expose zend_ce_operable / zend_ce_operand_side / zend_enum_OperandSide.
  • Add class InvalidOperatorError extends TypeError to Zend/zend_exceptions.stub.php and expose zend_ce_invalid_operator_error.
  • Preserve default class registration order in zend_register_default_classes() by registering OperandSide via zend_register_operand_side_ce() after zend_register_enum_ce().

2. Compile/Link-Time Validation (d9d11c32bb3)

  • 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, early-bound subclass, and inherited non-Operable ancestor methods): must be public, 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 global OperandSide for the optional 2nd parameter, and a required return type other than void, never, or mixed (no return by reference).
    • Pure explicit-final enforcement (no implicit fn_flags mutation): every concrete operator method on an Operable class must be declared final unless the class itself is final (abstract and interface methods are exempt; non-Operable ancestor methods must already be final and report the declaring class name; anonymous classes are not exempt because they can be extended via class_alias()).

3. Runtime Dispatch, InvalidOperatorError, and Optimizer/JIT (32d809f45dc)

  • Dispatch binary arithmetic operators (+, -, *, /, %, **), unary +/- (via __mul(1) / __mul(-1)), and compound assignments (+=, -=, *=, /=, %=, **=) in Zend/zend_operators.c:
    1. Left operand (Operable)
    2. Right operand (Operable with OperandSide::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 CE-cache + bounds-safe stack-buffered ZEND_FETCH_CLASS_NO_AUTOLOAD lookups so type checks never trigger autoloaders), plus zend_enum_get_case_by_id() for OperandSide cases.
  • Safe aliasing handling when result == op1 || result == op2 and clean exception propagation.
  • Scope zend_binop_error() to throw InvalidOperatorError (subclass of TypeError) only when an arithmetic operand implements Operable, preserving plain TypeError for non-Operable operands and bitwise/shift operators (zero BC break for existing code).
  • 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.

4. Reflection Support (28171f3b3a0)

  • Add ReflectionMethod::isOperatorMethod(): bool using intern->ce (case-insensitive matching for __add..__pow on the reflected Operable class/interface, so methods inherited from non-Operable parents report true via the Operable child and false via the non-Operable base).
  • Add ReflectionClass::getOperatorMethods(): array returning array<string, ReflectionMethod> keyed by operator symbol in deterministic order (+, -, *, /, %, **).

5. Test Coverage

  • Added 52 .phpt tests under Zend/tests/operable/ covering built-in symbols, compile/link validation, explicit final rules (including non-Operable parent hierarchies and anonymous class_alias() cases), runtime dispatch, unary +/-, type matching, long class names, compound assignments, error handling, Reflection, OPcache/JIT hot loops, and BcMath\Number / GMP interop.
  • Updated ext/reflection/tests/ReflectionClass_toString_001.phpt for ReflectionClass::getOperatorMethods().

Verification

  • Zend/tests/operable/: 51/51 passed (1 skipped: gmp).
  • ext/reflection/tests/: 568/568 passed (15 skipped).

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
ahmed-nour-dev force-pushed the rfc-typed-operator-methods branch from 2cf6e96 to 28171f3 Compare November 24, 2026 10:21
@ahmed-nour-dev ahmed-nour-dev changed the title [RFC] Typed Operator Methods (Operable, Side, InvalidOperatorError) [RFC] Typed Operator Methods (Operable, OperandSide, InvalidOperatorError) Nov 24, 2026
@ahmed-nour-dev

Copy link
Copy Markdown
Author

Reorganized the branch into 4 clean commits (92cbd775ddd, d9d11c32bb3, 32d809f45dc, 28171f3b3a0) and updated the PR description to reflect the latest design refinements:

  • Renamed global Side to OperandSide (OperandSide::Left, OperandSide::Right) across the engine, stubs, and tests to avoid userland naming collisions.
  • Replaced implicit ZEND_ACC_FINAL mutation with pure explicit-final validation: concrete operator methods on Operable classes must be declared final unless the class itself is final (abstract/interface methods are exempt; non-Operable ancestor methods must already be final and report the declaring class name; anonymous classes are not exempt so class_alias() cannot bypass non-overridability).
  • Updated ReflectionMethod::isOperatorMethod() to check intern->ce instead of mptr->common.scope so operator methods inherited from non-Operable parents report true via the Operable child and false via the non-Operable base.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants