PHP Internals News: Episode 94: Unwrap Reference After Foreach
In this episode of “PHP Internals News” I chat with Nikita Popov (Twitter, GitHub, Website) about the “First Class Callable Syntax” 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, the podcast dedicated to explaining the latest developments in the PHP language. This is Episode 94. Today I’m talking with Nikita Popov about the unwrap reference after foreach RFC that he’s proposing. Nikita, would you please introduce yourself?
- Nikita Popov 0:33
-
Hi, Derick. I’m Nikita and I work at JetBrains on PHP core development.
- Derick Rethans 0:38
-
So no changes compared to the last time.
- Nikita Popov 0:41
-
Yes, at the time before that.
- Derick Rethans 0:43
-
So what is the problem that is RFC is going to solve?
- Nikita Popov 0:46
-
Well, it’s really a very minor thing. I think it’s a relatively well known problem for the more experienced PHP programmers. It’s like a classic example, you have a foreach loop by reference. So foreach array as value by reference, and then you do a second loop after that, foreach array as value at the same it’s by value. So without the reference sign. The result of that is that your last two array elements are going to be the same, which is kind of unexpected. If you’re not familiar with how references in PHP work and scoping in PHP works. So I think it’s worth explaining what’s going on there.
- Derick Rethans 1:27
-
Can you quickly explain the scoping or rather the lack of it, I suppose?
- Nikita Popov 1:31
-
Yeah, it’s really the lack of PHP really only has function scoping. So if you have a foreach array as value, then the value variable is going to stay alive, even after the foreach loop. And usually, that won’t make much of a difference. So you will just have like reference to the last element of the array, might even be useful for some cases, you know, before we added the array, I think, array_key_last function. If the last element now is a reference, so if you have a reference to the last element, then you’re write into that variable is also going to modify the last element of the array. So if you now have a second foreach loop, using the same variable, that’s actually not just modifying that variable, but it’s also always modifying the last element of the array.
- Derick Rethans 2:15
-
Okay, just to clarify, it isn’t necessarily the last element in the foreach loop. It’s the last one that’s been assigned to?
- Nikita Popov 2:22
-
Yeah, that’s, that’s true.
- Derick Rethans 2:24
-
Is this not something that people actually use for some useful reasons?
- Nikita Popov 2:28
-
As mentioned before, technically, you could use it to get a reference to the last element and then modify the last element outside the foreach loop. I don’t think this is a particularly common use case. But I’m sure people have used in here there. This is a use case we would break with the proposed RFC.
- Derick Rethans 2:47
-
I think it is one I have used in the past,
Truncated by Planet PHP, read more at the original (another 14677 bytes)