rfc:user_defined_operator_overloads
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:user_defined_operator_overloads [2021/12/11 09:34] – typo jordanrl | rfc:user_defined_operator_overloads [2021/12/16 04:19] – Extra future scope jordanrl | ||
---|---|---|---|
Line 107: | Line 107: | ||
<code php> | <code php> | ||
// These all should result in an error | // These all should result in an error | ||
- | $obj->' | + | $obj->{' |
call_user_func([$obj, | call_user_func([$obj, | ||
call_user_func(' | call_user_func(' | ||
+ | </ | ||
+ | |||
+ | 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. | ||
+ | |||
+ | <code php> | ||
+ | // Manually creating a closure allows a direct function call | ||
+ | $closure = Closure:: | ||
+ | $closure(1, OperandPosition:: | ||
+ | |||
+ | // You can also retrieve the closure through Reflection | ||
+ | $closure = (new ReflectionMethod($obj, | ||
+ | $closure(1, OperandPosition:: | ||
+ | |||
+ | $closure = (new ReflectionObject($obj))-> | ||
+ | $closure(1, OperandPosition:: | ||
</ | </ | ||
Line 418: | Line 433: | ||
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 not 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(), | == Adding getOperators(), | ||
Line 656: | Line 671: | ||
==== Global Operator Overloads ==== | ==== Global Operator Overloads ==== | ||
This RFC deals with allowing each class to define its own interaction with operators. However, if overloading the operator itself were desired for the entire application, | This RFC deals with allowing each class to define its own interaction with operators. However, if overloading the operator itself were desired for the entire application, | ||
+ | |||
+ | ==== Functions for Operators ==== | ||
+ | Having functions for operators may be beneficial when objects which use operator overloads are used in conjunction with functions like '' | ||
+ | |||
+ | <code php> | ||
+ | array_reduce($arrOfObjs, | ||
+ | </ | ||
+ | |||
+ | These could be polyfilled in PHP currently: | ||
+ | |||
+ | <code php> | ||
+ | array_reduce($arrOfObjs, | ||
+ | </ | ||
+ | |||
+ | ==== Query Builder Improvements ==== | ||
+ | With some additional improvements, | ||
+ | |||
+ | <code php> | ||
+ | $qb-> | ||
+ | </ | ||
+ | |||
+ | ==== Enum Return Type For <=> ==== | ||
+ | |||
+ | Returning an enum for the <=> would be preferable for two reasons. | ||
+ | |||
+ | - It allows the function to return an equivalent of ' | ||
+ | - It is easier to read and understand the behavior in code, while integer values often require a moment to remember the meaning | ||
+ | |||
+ | This is listed as future scope because there is a separate RFC which covers this feature: https:// | ||
+ | |||
+ | It is listed as a separate RFC because it is something that could be delivered whether or not this RFC passes. | ||
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== |
rfc/user_defined_operator_overloads.txt · Last modified: 2022/01/17 01:16 by jordanrl