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
- Derick Rethans 0:16
Hi, I’m Derick. And this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. This is Episode 42. Today I’m talking with Jan Böhmer about Userspace Operator Overloading. Jan, would you please introduce yourself?
- Jan Böhmer 0:33
Hi, my name is Jan Böhmer. I’m a physics student from Germany. And I’m the author of the Operator Overloading RFC.
- Derick Rethans 0:40
What brought you to writing this RFC?
- Jan Böhmer 0:42
Mostly because I have worked with monetary objects in the past. And it was a bit tedious to work when it comes to calculating. And whenever you have to want to calculate something, you have to call functions on objects. This is not possible to call, just use operators like with normal values like floats or integers.
- Derick Rethans 1:06
Because the monetary objects themselves had multiple things embedded in there or something like that?
- Jan Böhmer 1:11
Yes, they describe mostly a value and a currency. And together they are saved in an object.
- Derick Rethans 1:18
Okay that that seems like a reasonable thing to do, right? I mean, other times people say the same thing about doing complex numbers or something like vectors. The RFC is called Userspace Operator Overloading. What is operator overloading?
- Jan Böhmer 1:31
Yeah. Basically, is the idea that you can define operators, like addition or subtraction, or the string concatenation for objects
- Derick Rethans 1:43
Does PHP already have something like this?
- Jan Böhmer 1:45
Actually, yes. Objects can have something that calls do operation handler. This is called whenever PHP encounters an object, but if used with an operator. The problem is that this handler is only available for PHP internally. So if you want to use it, you have to write an extension.
- Derick Rethans 2:06
So it will be possible to have in an extension a Monetary class with its own operators already defined on it.
- Jan Böhmer 2:14
Exactly PHP extension GMP uses this as already. The problem is that it’s not very flexible, you already have to know, be familiar with C, you have to be able to compile that. You have to contribute it to whatever system you want to use it. Since we have the foreign function interface since PHP 7.4 we can implement many things without to actually have an extension. But this operator overloading is something that’s not possible yet inside from PHP.
- Derick Rethans 2:47
So it wouldn’t have been possible to write the GMP extension which is of course a wrap around libgmp with FFI, because there’s no operator overloading available in PHP.
- Jan Böhmer 2:59
Not in that comfortable way. You could use this way with functions but it would a bit more tedious then using
Truncated by Planet PHP, read more at the original (another 13038 bytes)