PHP Internals News: Episode 41: __toArray()
In this episode of “PHP Internals News” I chat with Steven Wade (Twitter, GitHub, Website) about the __toArray() 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:16
-
Hi, I’m Derick. And this is PHP internals news, a weekly podcast dedicated to demystifying the development of the PHP language. Hi, this is Episode 41. Today I’m talking with Stephen Wade about an RFC that he’s produced, called __toArray(). Hi, Steven, would you please introduce yourself?
- Steven Wade 0:35
-
Hi, my name is Steven Wade. I’m a software engineer for a company called follow up boss. I’ve been using PHP since 2007. And I love the language. So I wanted to be able to give back to it with this RFC.
- Derick Rethans 0:48
-
What brought you to the point of introducing this RFC?
- Steven Wade 0:50
-
This is a feature that I’ve I’ve kind of wish would have been in the language for years, and talking with a few people who encouraged it’s kind of like the rule of starting a user group right? If there’s not one and you have the desire, then you’re the person to do it. A few people encouraged and say: Well, why don’t you go out and write it. So I’ve spent the last two years kind of trying to work up the courage or research it enough or make sure I write the RFC the proper way, and then also actually have the time to commit to writing it and following up with any of the discussions as well.
- Derick Rethans 1:18
-
Okay, so we’ve mentioned the word RFC a few times. But we haven’t actually spoken about what it is about. What are you wanting to introduce into PHP?
- Steven Wade 1:25
-
I want to introduce a new magic method. The as he said, the name of the RFC is the __toArray(). And so the idea is that you can cast an object, if your class implements this method, just like it would toString(). If you cast it manually to array then that method will be called if it’s implemented. Or as, as I said, in the RFC, array functions will it can it can automatically cast that if you’re not using strict types.
- Derick Rethans 1:49
-
Oh, so only if it’s not strictly typed. So if its weakly typed would call the toArray() method if the function’s argument or type hint array.
- Steven Wade 1:58
-
Yes, and that is actually something that came up during the discussion period, which is something again, this is why we have discussions, right? Is to kind of solicit feedback on things we don’t think about it, we may overlook or, and so someone did point out that it is, you know, it would not function that way, or you would not expect it to be automatically cast for you, if you’re using strict types.
- Derick Rethans 2:17
-
Okay.
- Steven Wade 2:18
-
The RFC has been updated to reflect that as well.
- Derick Rethans 2:20
-
So now the RFC says it won’t be automatically called just for type hint.
- Steven Wade 2:24
-
Correct.
- Derick Rethans 2:24
-
Not everybody is particularly fond of magic methods. What would you say about the criticism that introducing even more of them would be sort of counterproductive,
Truncated by Planet PHP, read more at the original (another 16164 bytes)