On Sun, Jan 3, 2016 at 3:14 AM, Sara Golemon <poll...@php.net> wrote:
> Patricio Tarantino has asked me to help him propose Operator > Overloading in PHP 7.1 (based in part on my operator extension in > PECL). I think we can expose this to usespace as magic methods with > very little overhead (the runtime check and dispatch is already there, > after all). > > I do think that the "Future Expansion" section bears following through > with as well, but the basic set of methods already hooked for GMP > would be a nice start. > > https://wiki.php.net/rfc/operator-overloading > > -Sara > Thanks for the proposal, Sara! A few questions to clarify: 1. If an object implements __add__, will $a += $b be equivalent to $a = $a->__add($b) and ++$a be equivalent to $a = $a->__add(1)? This is how internal operator overloading currently works (we have no separate overloads for assign ops). Will this RFC also work this way? 2. Regarding associativity, the RFC states "When both operands to a binary expression implement operator overloading, the left-hand operand's handler will be invoked. If only one of the two implement overloading, then that side is invoked with the relative operation inverted as necessary." This sounds rather fishy to me, as not all operations are abelian. In particular __sub, __div, __pow and __concat are *usually* non-commutative. As such, the current RFC does not appear to leave room for supporting things like 1/$complex or 1-$complex or e**$complex. As such I wonder if these magic methods oughtn't be static methods with two operands. 3. Relatedly, consider the situation where $obj1 op $obj2, where $obj1 and $obj2 are instances of different classes, both of which have distinct overloads of the op operator. It so happens that the op-implementation of $obj1 does not support operations against $obj2, while the op-implementation on $obj2 does support operations against $obj1. The op-implementation of $obj1 will be invoked first -- will there be some way for it to signal "I don't support this operation", thus allowing the op-implementation of $obj2 to be used? Thanks, Nikita