PHP Internals News: Episode 80: Static Variables in Inherited Methods – Derick Rethans

PHP Internals News: Episode 80: Static Variables in Inherited Methods

In this episode of “PHP Internals News” I chat with Nikita Popov (Twitter, GitHub, Website) about the “Static Variables in Inherited Methods” 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 explain the latest developments in the PHP language. This is episode 80. In this episode I speak with Nikita Popov again about another RFC that he’s proposing. Nikita, how are you doing today?

Nikita Popov 0:30

I’m still doing fine.

Derick Rethans 0:33

Well, that is glad to hear. So the reason why you saying, I’m still doing fine, is of course because we basically recording two podcast episodes just behind each other.

Nikita Popov 0:41

That’s true.

Derick Rethans 0:42

If you’d be doing fine 30 minutes ago and bad now, something bad must have happened and that is of course no fun. In any case, shall we take the second RFC then, which is titled static variables in inherited methods. Can you explain what is RFC is meant to improve?

Nikita Popov 1:00

I’m not sure what this meant to improve, it’s more like trying to fix a bug, I will say. This is a really, like, technical RFC for an edge case of an edge case, so I should say first, when I’m saying static variables, I’m not talking about static properties, which is what most people use, but static variables inside functions. What static variables do unlike normal variables, is that they persist across function calls. For example, you can have a counter static $i equals zero, and then increment it, and then each time we call the function, it gets incremented each time and the value from the previous call is retained. So that’s just the context of what we’re talking about.

Derick Rethans 1:43

Why would people make use of static variables?

Nikita Popov 1:46

I think one of the most common use cases is memoization.

Derick Rethans 1:50

Can you explain what that is?

Nikita Popov 1:51

If you have a function that that computes some kind of expensive result, but which is always the same, then you can compute it only once and store it inside the static variable, and then return it. Maybe possibly keyed by by the function arguments, but that’s the general idea. And this also works if it’s a free standing function. So if it’s not in the method where you could store state inside the static property or similar, but also works inside a non method function.

Derick Rethans 2:22

The keyword here in his RFC’s title is inherited methods, I suppose. What happens currently there?

Nikita Popov 2:29

There are a couple of issues in that area. The key part is first: How do static variables interact with methods at all? And the second part is how it interacts with inheritance. So first if you have an instance method, with a static variable, then some people expect that actually each object instance gets a separate static variable. This is not the case. The static variables are really bound to functions or methods, they do not depend on the object instanc

Truncated by Planet PHP, read more at the original (another 13509 bytes)