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 revisionBoth sides next revision
rfc:user_defined_operator_overloads [2021/12/16 04:19] – Extra future scope jordanrlrfc:user_defined_operator_overloads [2021/12/17 01:53] – Making operator callable jordanrl
Line 101: Line 101:
 This is avoided by allowing the restrictions on operator names to be separated from the restrictions on function names. This is avoided by allowing the restrictions on operator names to be separated from the restrictions on function names.
  
-=== Non-Callable ===+=== Callable ===
  
-Operand implementations cannot be called on an instance of an object the way normal methods can.+Operand implementations can be called on an instance of an object the way normal methods can.
  
 <code php> <code php>
-// These all should result in an error+// These all will work normally 
 +$op = '+'; 
 +$callable = [$obj, '+']; 
 + 
 +// Calls on the object variable
 $obj->{'+'}(1, OperandPosition::LeftSide); $obj->{'+'}(1, OperandPosition::LeftSide);
 +$obj->$op(1, OperandPosition::LeftSide);
 +$callable(1, OperandPosition::LeftSide);
 +
 +// Calls using call_user_func
 call_user_func([$obj, '+'], 1, OperandPosition::LeftSide); call_user_func([$obj, '+'], 1, OperandPosition::LeftSide);
 +call_user_func($callable, 1, OperandPosition::LeftSide);
 +
 +// This will error since + is not static
 call_user_func('ObjClass::+', 1, OperandPosition::LeftSide); call_user_func('ObjClass::+', 1, OperandPosition::LeftSide);
 </code> </code>
  
-They can be directly invoked with a Closure however. This is to fully support Reflection, and to allow direct calls while still making it obvious to PHP developers that they should not be treating these as normal methods.+They can be also be directly invoked with a Closure however. This fully supports Reflection, and allows direct calls.
  
 <code php> <code php>
rfc/user_defined_operator_overloads.txt · Last modified: 2022/01/17 01:16 by jordanrl