rfc:user_defined_operator_overloads

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:user_defined_operator_overloads [2021/12/08 18:15] – copy edit jordanrlrfc:user_defined_operator_overloads [2021/12/11 12:26] – copy edit jordanrl
Line 107: Line 107:
 <code php> <code php>
 // These all should result in an error // These all should result in an error
-$obj->'+'(1, OperandPosition::LeftSide);+$obj->{'+'}(1, OperandPosition::LeftSide);
 call_user_func([$obj, '+'], 1, OperandPosition::LeftSide); call_user_func([$obj, '+'], 1, OperandPosition::LeftSide);
 call_user_func('ObjClass::+', 1, OperandPosition::LeftSide); call_user_func('ObjClass::+', 1, OperandPosition::LeftSide);
Line 338: Line 338:
 Because comparisons have a reflection relationship instead of a commutative one, the $operandPos argument is omitted as it could only be used for evil (making ''$obj == 5'' have a different result than ''5 == $obj''). Because comparisons have a reflection relationship instead of a commutative one, the $operandPos argument is omitted as it could only be used for evil (making ''$obj == 5'' have a different result than ''5 == $obj'').
  
-Comparison operators do not throw the ''InvalidOperatorError'' when unimplemented. Instead, the PHP engine falls back to existing comparison logic in the absence of an override for a given class.+Equality and comparison operators do not throw the ''InvalidOperatorError'' when unimplemented. Instead, the PHP engine falls back to existing comparison logic in the absence of an override for a given class.
  
 The signature for the equals operator has the additional restriction of returning ''bool'' instead of ''mixed''. The signature for the equals operator has the additional restriction of returning ''bool'' instead of ''mixed''.
Line 352: Line 352:
 Any return value larger than 0 will be normalized to 1, and any return value smaller than 0 will be normalized to -1. Any return value larger than 0 will be normalized to 1, and any return value smaller than 0 will be normalized to -1.
  
-The $operandPos argument is omitted as it could only be used for evil e.g. implementing different comparison logic depending on which side its on. Instead of passing $operandPos the engine will multiply the result of the call by (-1) where appropriate:+The $operandPos argument is omitted as it could only be used for evil e.g. implementing different comparison logic depending on which side it'on. Instead of passing $operandPos the engine will multiply the result of the call by (-1) where appropriate:
  
 <code php> <code php>
Line 412: Line 412:
 Several changes to reflection must be made to support this feature. Several changes to reflection must be made to support this feature.
  
-=== Changes to getMethods(), getMethod(), and hasMethod() ===+=== Changes To ReflectionClass === 
 + 
 +== Changes to getMethods(), getMethod(), and hasMethod() ==
  
 These methods need to be updated to ignore the operator methods. Since these are stored internally like any other function on the class entry, they need to be filtered from the results. These methods need to be updated to ignore the operator methods. Since these are stored internally like any other function on the class entry, they need to be filtered from the results.
  
-The reason for removing the operators from this result is because the operator methods are not callable with string literals on the object. Since they cannot be called like a method is, the should be returned with the other methods on a class.+The reason for removing the operators from this result is because the operator methods are not callable with string literals on the object. Since they cannot be called like a method is, they should not be returned with the other methods on a class.
  
-=== Adding getOperators(), getOperator(), and hasOperator() ===+== Adding getOperators(), getOperator(), and hasOperator() ==
  
 These methods must be added to interact with the function handlers for the operator implementations. They will act as an inverse to the changes above. These methods must be added to interact with the function handlers for the operator implementations. They will act as an inverse to the changes above.
  
 Operator methods will be represented by an instance of ''ReflectionMethod'', since in most respects they can be treated like a normal method for the purposes of reflection. Operator methods will be represented by an instance of ''ReflectionMethod'', since in most respects they can be treated like a normal method for the purposes of reflection.
 +
 +=== Changes To ReflectionMethod ===
 +
 +== New Method isOperator() ==
 +
 +Returns true if the method being reflected uses the ''operator'' keyword. Returns false otherwise.
  
 ===== FAQ ===== ===== FAQ =====
Line 515: Line 523:
 } }
  
-$result = new Money(5, 'USD') + new Vextor2d(5, 10);+$result = new Money(5, 'USD') + new Vector2d(5, 10);
  
 // Type error, Vector2d can't be used as Money // Type error, Vector2d can't be used as Money
Line 614: Line 622:
 // The rest of the extension's do_operation handler // The rest of the extension's do_operation handler
 </code> </code>
 +
 +To further help extensions support this feature, there are two helper functions:
 +
 +<code c>
 +int has_overload = zend_std_has_op_overload(opcode, &zval);
 +
 +zend_function overload_method = zend_std_get_op_overload(opcode, &ce);
 +</code>
 +
 +It is safe to pass any zval pointer to ''zend_std_has_op_overload()'', as it first checks whether or not the ''Z_TYPE_P(zval) == IS_OBJECT'' and returns 0 if it doesn't.
  
 ==== To Opcache ==== ==== To Opcache ====
rfc/user_defined_operator_overloads.txt · Last modified: 2022/01/17 01:16 by jordanrl