Nikolay Andonov

Hi, I'm Nikolay

Web Developer, Wannabe Doer

Unpopular PHP 7+ Features

16th of May, 2020

Let's talk about some unpopular PHP 7+ features. Two weeks ago I wrote an article about unpopular PHP features so go check it out if that's sounds interesting to you. This is basically a follow-up, but I decided to share PHP 7 features only in here.

Iterable instead of array for type hints

Sometimes we have to write a function where we loop through an argument that's being passed in. And if that's the only thing we do in the function with that argument, I'd say it's better to type hint with Iterable instead of array . Let's take a look

When we do that, our users (ie the programmers who would use our function) can pass not just plain arrays , but also objects who know how to traverse themselves (fe if the class they are instantiated from implements the Iterator interface).

One downside of that approach however is the fact that we can't use functions like array_key_exists() because they only support arrays.

Iterable was introduced in PHP 7.1, you can find more about it in the official PHP docs

Anonymous classes

I've seen many functions in the wild which returns an associative array.

I personally don't like this approach. I don't wanna be that "SOLID or die" guy, but for cases like this I feel the data is not encapsulated, and our users have to peak at our implementation so they know how to use the data. I don't know, something smells in here. Most of the times the solution here is Value Objects and you would obviously have to find a place in your directory tree for them. And if that's hard for you (btw welcome to the club 👋🏼) and you only need this for a single place in your codebase, say "hi" to the anonymous classes

Boy, is that a lot of boilerplate!? Hell yeah. But not only we encapsulated the data, we also have a special place to format the data, and we also have a place where we can extract only some parts of the data (notice the getTopStats() example). And our users will be auto-completed by their IDEs with everything they can do with the data that's being returned. That's a win for me.

This feature was implemented in PHP 7, here is a link to the official docs in case you want to explore more.

Grouped namespaces

If we have to import two or more classes from the same namespace, we can group them together on the same line.

It's obvious and nothing new for a lot of people, but I've worked with codebases where this feature wasn't used at all. So I decided to squeeze it in real quick here.

It's worth mentioning that we can also nest as many levels deep as we want when importing with single use statement. However, what may be new to you is the fact that a compound namespaces with a depth of more than two must not be used if we want to be PSR-12 compatible. Take a look at the PSR itself for good examples of what I just tried (and probably failed 🤓) to explain.

More to come

You can expect at least one more article in this series in the next few weeks which will mostly cover pre PHP 7 features, similar to the first article in the series. Thanks for the read.

Wanna keep in touch?

I send out an email once every 30-60 days about stuff similar to the thing you've just read. If that sounds interesting to you, I'd love to have you on board.