Re: [Pre-RFC Discussion] User Defined Operator Overloads (again)
| From: | Rob Landers | Date: | Wed, 18 Sep 2024 00:02:52 +0000 |
| Subject: | Re: [Pre-RFC Discussion] User Defined Operator Overloads (again) | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125608@lists.php.net to get a copy of this message | ||
On Wed, Sep 18, 2024, at 01:12, K Sandvik wrote:
> Operator overloading is indeed a very overloaded topic concerning computer languages.
>
> But my 0.02 cents is that it's a good thing to have provided it solves one or more of the
> problems such as:
Hey Kent,
> * Will the code be easier to write, maintain, and read?
Just imagine being able to ignore the whole DateInterval class. Maybe one day we could write
$datetime + ‘2 minutes’ and it “just work” instead of writing PT2M or using special
libraries..
Another example is to consider how most people do time durations in php: 5 * MINUTE_IN_SECONDS. or
something like this. They are usually stored as integers. If you had a units library of time, you
could write 5 * MINUTE where MINUTE is a one minute unit object (Which also begs the question of
whether operators are “constant expressions” when defined…) so that a unit object is stored
instead of an integer, preventing mistakes or having to dig through the code to find out what units
“$timeout” is in.
> * Will this help with optimizations?
I’m not sure what you mean by this.
> * Will this provide new paradigms that are useful for architectural solutions?
I think it could. For example a Host + Port + Path objects could result in a URI object. Or a DI
container could use it to express service dependencies more succinctly.
> * or anything else practical and not based on the idea that 'php needs it as it's
> available in other languages?
>
>
> --Kent
I think it is worth pointing out that you can already do “operator overloading” in PHP, so long
as your operators are emojis and you don’t mind how it looks… 😂
https://3v4l.org/gQKTb
In all seriousness though, php has classes because other languages have classes. It’s a valid
argument but maybe not a strong one.
— Rob