Unpopular PHP Features, Part 1
There are some features in PHP which for one reason or another we all don't use that much. In this series of articles I will be providing real-world examples of how we can apply more the features in question.
Static variables inside the body of a function
Class properties being declared as static is nothing new. But did you know that we can declare static variables inside the body of a function?
Consider that we call the surprise() method from several different places in our app. The line that's commented out would be executed just once for the lifetime of the request (in HTTP context) / script execution (in CLI context)
If that function was a class method, it would behave the exact same way as it’s being declared as a static property on the class level. Ie. if we use it inside a non-static method, and we call that method on two different instances of the class, PHP will use the same variable for both. So the only thing we get by declaring it inside the body of the function rather than the class itself, is that only the current function will have access to the variable. So yeah, most probably that's not a good use case for them.
The interesting part however is we can use static variables inside regular functions as well (outside classes). And what’s more fascinating for me is that they work inside closures, too. I remember the first time I saw this feature, it was used exactly inside a closure. The default Laravel UserFactory file used to ship with that in the past. They needed it so the bcrypt function is called only once even if you seed thousands of records.
I also find this feature very useful when I want to lazy load a database query, and to make sure it's not called more than once per request. Sure, there are other ways to achieve the same thing, but I like this approach because it’s short and clean.
The goto keyword
This is one of the ways we can write recursion in PHP. Say we have a function, we want the first few lines to always be executed, and then we want to recursively execute the rest of the function. We can either extract everything but the first few lines to a new, recursive function. Or we can be lazy and use the goto feature.
Another thing we can do with this syntax is to conditionally skip parts of the code. Not sure I can think of a good real-world use case for that, but it's worth mentioning.
Skipping nested loops
Both continue and break are well known keywords. But what's less known is the fact we can suffix them with a number. So when we are dealing with nested loops, we can instruct PHP to the exact parent we want to apply the keyword to.
Fun stuff, right? It's probably a better idea to avoid nesting loops by extracting each loop into its own function. But yeah, alternatively we have this weird syntax we can take leverage of.
More to come
I got more unpopular PHP features which I'd like to share. But for the sake of keeping things shorter, I am going to write another article in the next week or two. And I am not sure if the second article will be the last one in the series. Sooo... stay tuned.
I've just written the second part of the series. It's about PHP 7+ features only