> Am 05.01.2016 um 15:29 schrieb Alexander Lisachenko <lisachenko...@gmail.com>:
> 
> 2016-01-05 2:04 GMT+03:00 Andrea Faulds <a...@ajf.me>:
> 
>> I agree that we could do something with interfaces. I would like to point
>> out that we actually already have an example of this, in the form of the
>> \ArrayAccess interface, which requires you to implement all the different
>> indexing operations at once. Unfortunately, though, \ArrayAccess doesn't
>> give us a precedent for dealing with the $this issue (in `$a + $b`, who
>> gets called, how do we handle differing types, etc?), but it's a start.
>> 
> 
> 
> Hi, Andrea and internals team!
> 
> Interface is a good way to implement new functionality for classes, but
> operator overloading is language feature itself, so from my point of view,
> it will be better to put this functionality into the magic method.
> Personally, I don't like hundreds of __add, __mul, __etc methods, because
> this will be ugly and will require addition of new methods when new
> operators are included (eg __pow).
> 
> I want to suggest to add only one single method:
> 
> public function __operator(int $operatorKind, ...$operatorArgs);
> 
> This method will be called for every operator and class can check the
> $operatorKind variable with simple check: if (Php\Operator::OP_DIVISION ===
> $operatorKind) return $operatorArgs[0]->value / $operatorArgs[1]->value;
> New values for operator type enumeration can be added later into new
> versions of PHP easily.

I think an interface is a strongly superior way, especially as it'll allow you 
to hint that an implementation became incomplete when an operator has been 
added. (Also, how often are relevant operators added?)
At least as you aren't supposed to *ab*use the methods for other things than 
they're intended to. If you implement number operations, then you should 
implement the whole set of them.
If you implement a string class, you should implement the array access and the 
string concat.

Operators definitely shouldn't have specific context sensitive meaning (like 
printing when you bitshift (wtf, C++?)).
At least, we should *assume* that the operators are used in accord with their 
intended meaning.

Bob
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to