RE: [PHP-DEV] Re: [VOTE] Userspace operator overloading
| From: | jan dot h dot boehmer at gmx dot de | Date: | Thu, 26 Mar 2020 16:37:02 +0000 |
| Subject: | RE: [PHP-DEV] Re: [VOTE] Userspace operator overloading | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109340@lists.php.net to get a copy of this message | ||
On Thursday, March 26, 2020 3:50 AM Jakob Givoni <jakob@givoni.dk> wrote:
> On Wed, Mar 25, 2020 at 6:28 AM Christoph M. Becker <cmbecker69@gmx.de> wrote:
>
>> It seems to me that the RFC is not sufficiently specific enough
>> regarding the concatenation of instances of classes which implement
>> __toString().
>
> Exactly what I was thinking too. Would be nice with some examples on this.
I am not sure if I can (or should) still add this to the RFC, here some clarification on this:
The overloaded concat operator has higher priority than the __toString() method.
So if Class A overloades the concat operator, then calling $a . $b means ClassA::__concat($a, $b);
(Note that both operands are passed in their original form)
If you want to concat the string representations, you will have to explicitly convert the objects to
strings:
$ret = (string) $a . (string) $b;
If the concat operator is not overloaded, the behavior is like now, and the objects are converted
implicitly to strings (so $a . $b actually means (string) $a . (string) $b).
Furthermore an notice is triggered, hinting the user that he could overload the concat operator.
(Maybe here a different message than for the other operators would be useful).