rfc:userspace_operator_overloading

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
rfc:userspace_operator_overloading [2020/03/19 10:40] jbtronicsrfc:userspace_operator_overloading [2020/04/06 19:31] (current) jbtronics
Line 3: Line 3:
   * Date: 2020-02-01   * Date: 2020-02-01
   * Author: Jan Böhmer, jan.h.boehmer@gmx.de   * Author: Jan Böhmer, jan.h.boehmer@gmx.de
-  * Status: Under Discussion +  * Status: Declined 
-  * First Published athttp://wiki.php.net/rfc/userspace_operator_overloading+  * Target VersionPHP 8.0 
 +  * Implementation: https://github.com/php/php-src/pull/5156 
 ===== Introduction ===== ===== Introduction =====
 At the moment expressions like ''$a + $b'' or ''$a * 2'' are only valid if ''$a'' and ''$b'' are scalar types like int or float. However, in many other programming languages like Python, C# or C++ it is possible to overload these operators to use them on classes, to implement custom math (and other) objects. This RFC proposes userspace operator overloading for PHP. At the moment expressions like ''$a + $b'' or ''$a * 2'' are only valid if ''$a'' and ''$b'' are scalar types like int or float. However, in many other programming languages like Python, C# or C++ it is possible to overload these operators to use them on classes, to implement custom math (and other) objects. This RFC proposes userspace operator overloading for PHP.
Line 43: Line 44:
 // Equivalent to $x = Vector3::__add($a, $b) // Equivalent to $x = Vector3::__add($a, $b)
 $x = $a + $b; $x = $a + $b;
-//Equivalent to $y = Vecotr3::__mul(3, $b)+//Equivalent to $y = Vector3::__mul(3, $b)
 $y = 3 * $b; $y = 3 * $b;
 </PHP> </PHP>
Line 82: Line 83:
 ^ Positive value | ''+ $a'' | interpreted as **''(+1)*$a''**, can be overloaded by implementing %%__mul%% | ^ Positive value | ''+ $a'' | interpreted as **''(+1)*$a''**, can be overloaded by implementing %%__mul%% |
 ^ Shorthand assignment | ''+='', ''*='', ''.='', etc. | ''$a += $b'' is interpreted as ''$a = $a + $b'', can be overloaded by implementing %%__add%% | ^ Shorthand assignment | ''+='', ''*='', ''.='', etc. | ''$a += $b'' is interpreted as ''$a = $a + $b'', can be overloaded by implementing %%__add%% |
-^ Increment | ''$a++'' | interpreted as ''$a = $a + 1'', can be overloaded by implementing %%__add%% | +^ Increment | ''$a++'' (and ''++$a''| interpreted as ''$a = $a + 1'', can be overloaded by implementing %%__add%% | 
-^ Decrement | ''$a--''  | interpreted as ''$a = $a - 1'', can be overloaded by implementing %%__sub%% |+^ Decrement | ''$a%%--%%'' (and ''%%--%%$a'' | interpreted as ''$a = $a - 1'', can be overloaded by implementing %%__sub%% |
  
 === Operators that can not be overloaded === === Operators that can not be overloaded ===
 The following operators can not be overloaded by using this method (neither explicit or implicit): The following operators can not be overloaded by using this method (neither explicit or implicit):
 |                       ^ Operator                                                   | |                       ^ Operator                                                   |
-| Comparision operators | ''<'', ''<='', ''>'', ''=='' etc.       | maybe subject of a future RFC |+| Comparision operators | ''<'', ''%%<=%%'', ''>'', ''=='' etc.       | maybe subject of a future RFC |
 | Assignment operator   | ''='' | | | Assignment operator   | ''='' | |
 | Logic operators        | ''||'', ''!'', ''&&'' | | | Logic operators        | ''||'', ''!'', ''&&'' | |
Line 95: Line 96:
 | Tenary operator       | ''? :'' | | | Tenary operator       | ''? :'' | |
 | Error control operator | ''@'' | | | Error control operator | ''@'' | |
-| Object access operator | ''->'' |  |+| Object access operator | ''%%->%%'' |  |
  
 ==== Evaluation order ==== ==== Evaluation order ====
Line 111: Line 112:
  
 ==== Error handling ==== ==== Error handling ====
-If an operator is used with an object that does not overload this operator, an NOTICE is triggered, which gives the user a hint about the method that has to be overloaded. For backward compatibility objects, which do not overload the operator, are converted to integer 1 (current behavior).+If an operator is used with an object that does not overload this operator, an NOTICE is triggered (to not break existing code), which gives the user a hint about the method that has to be overloaded. For backward compatibility objects, which do not overload the operator, are converted to integer 1 (current behavior).
  
 If the class overloads the operator, and the magic method do not return a value, an ERROR is triggered. If the class overloads the operator, and the magic method do not return a value, an ERROR is triggered.
Line 153: Line 154:
  
 ==== Immutable types ==== ==== Immutable types ====
-To ensure that objects can not be changed (which could cause undesirable behavior), immutable objects (see this [[rfc:immutability|old RFC]] could be helpful.+To ensure that objects can not be changed (which could cause undesirable behavior), immutable objects (see this [[rfc:immutability|old RFC]]could be helpful.
  
 ==== Allow overloading of shorthand assignment operators and increment/decrement operators ==== ==== Allow overloading of shorthand assignment operators and increment/decrement operators ====
Line 160: Line 161:
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
 Add userspace operator overloading as described: yes/no Add userspace operator overloading as described: yes/no
 +
 +===== Vote =====
 +Voting started 2020-03-23 and ends 2020-04-06.
 +<doodle title="Add userspace operator overloading as described?" auth="jbtronics" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
-Draft implementation can be found here: https://github.com/php/php-src/pull/5156+Implementation can be found here: https://github.com/php/php-src/pull/5156
  
 ===== References ===== ===== References =====
Line 168: Line 176:
    * [[https://externals.io/message/108300|First email discussion]]    * [[https://externals.io/message/108300|First email discussion]]
    * [[rfc:operator-overloading|Old RFC with an similar propose]]    * [[rfc:operator-overloading|Old RFC with an similar propose]]
 +   * [[https://externals.io/message/108608|Discussion part 1]]
 +   * [[https://externals.io/message/108788|Discussion part 2]]
  
-===== Rejected Features =====+===== Rejectected features ===== 
 +  * Use interfaces instead of magic methods 
 +  * Use type hints to declare supported types (this would introduce some special kind of function overloading)
  
 ===== Changelog ===== ===== Changelog =====
rfc/userspace_operator_overloading.1584614441.txt.gz · Last modified: 2020/03/19 10:40 by jbtronics