PHP Internals News: Episode 96: User Defined Operator Overloads
PHP Internals News: Episode 96: User Defined Operator Overloads
In this episode of "PHP Internals News" I chat with Jordan LeDoux (GitHub) about the "User Defined Operator Overloads" RFC.
The RSS feed for this podcast is https://derickrethans.nl/feed-phpinternalsnews.xml, you can download this episode's MP3 file, and it's available on Spotify and iTunes. There is a dedicated website: https://phpinternals.news
Transcript
- Derick Rethans 0:14
-
Hi, I'm Derick. Welcome to PHP internals news, a podcast dedicated to explaining the latest developments in the PHP language. This is episode 96. Today I'm talking with Jordan, about a user defined operator overloads RFC that he's proposing. Jordan, would you please introduce yourself?
- Jordan LeDoux 0:33
-
My name is Jordan LeDoux. I've been working in PHP for quite a while now. This is the second time I have ventured to propose an RFC.
- Derick Rethans 0:44
-
What was the first one?
- Jordan LeDoux 0:45
-
The first one was the "never for parameter types", which was much more exploratory. And we talked about it a little bit. And it generated a lot of good discussion that contributed to kind of the idea formation, which was what I hope to get out of it.
- Derick Rethans 1:01
-
Okay, but that didn't end up making it into a PHP release. As far as I understand, right?
- Jordan LeDoux 1:07
-
No, I withdrew it actually, it was clear that the better way to approach the problem it was trying to solve was with a much more comprehensive solution. That particular solution was something that only required a seven line change to the engine. So I wanted to see if it was something people were okay with, or thought was a decent idea for that particular problem, much more comprehensive, like template classes, or something like that is probably the better route to go.
- Derick Rethans 1:35
-
Well, I think the RFC that we're talking about today, is going to require quite a bit more than seven lines of code?
- Jordan LeDoux 1:41
-
Quite a bit more. Yeah.
- Derick Rethans 1:42
-
So what is this RFC that we're talking about today?
- Jordan LeDoux 1:45
-
Well, user defined operator overloads is a way for PHP developers to define the ways in which objects interact with specific operators. So for instance, the plus operator, the plus sign. It's a way for those objects to kind of define their own logic as far as how that's handled, which right now, as of PHP 8.0, those were all switched to type errors. So it's not possible currently to write any code that doesn't result in a fatal error, where objects are used with operators.
- Derick Rethans 2:25
-
Usually, I ask about every RFC, what problem are you trying to solve this? So what problem are you trying to solve this RFC?
- Jordan LeDoux 2:31
-
The biggest problem that this solves is that objects contain, so objects in most programs represent a value or multiple values that have a program context. That's the most powerful thing about objects is they're contextual, and they understand the state, they understand what state the object is in, and sometimes even what state the whole program is in. And that's necessary for a lot of things. Like for instance, if you're tracking a distance, you know, you might measure that meters, and that would have a number you might have 30 meters of distance, but it also has a unit of meters. You could just represent that as an int. And
Truncated by Planet PHP, read more at the original (another 32660 bytes)