PhpStorm 8 comes with PHP 5.6 support. With this latest PHP version released end of August, we don’t want to keep you from using new language features like exponentiation via **, use function and use const, constant expressions, variadic functions, argument unpacking and so on. In this post, we’ll have a look at a few interesting new language features. Visit the PHP website to learn about all of them.
Setting the PHP Language Level
To enable PHP 5.6 for a project, we can go to Project Settings | PHP and select the PHP 5.6 language level. Of course, to run PHP 5.6 code we will also have to add an interpreter that supports this language level.
An inspection is there as well. When using language features that are not compatible with the configured language level, PhpStorm will tell us and offer to set the language level for us.
Once done, PhpStorm lets us use the new language features. Let’s explore a few.
Variadic Functions in PhpStorm
One of the interesting new features in PHP 5.6 is support for variadic functions. Wait, what’s a variadic function? Glad you asked! It’s a function which accepts a variable number of arguments. For example, a lot of mathematical functions like sum(), multiply() or average() could apply to 1, 2 or hundreds of arguments. String concatenation is another example, as well as PHP’s internal functions like printf().
Here’s how we could create our own variadic functions in PHP prior to version 5.6:
See what is happening there? We’re calling into func_get_args() to get an array of all arguments passed into the function and return them all concatenated. There’s a problem with this code though: from seeing the function signature, we can’t tell if it accepts a variable number of arguments. Looking at that code tells us that it does not accept any arguments!
It gets worse… Consider the following:
Adding the $separator argument means it gets more confusing for users of our code. Does it take one argument? Or more? In our function body, we now also have to strip off the first argument using array_slice(func_get_args(), 1), otherwise we’d be concatenating $separator as well.
This is where PHP 5.6 variadics come in handy:
We now have a function signature that is clear to our users: it takes a fixed argument and a variadic argument. In our function body, we don’t have to do any logic with func_get_args(). All function arguments are just there the way we’d expect them.
The __debugInfo() magic method
While PhpStorm comes with great debugging support, there are cases where we may want to see additional information about a class we’re working with. For example when working with streams in PHP, all the debugger will tell us is that it’s a resource of type stream, and what the ID is.
Using var_dump(), we don’t get any more information:
What if we could influence the debugger data that is being returned in a class? With PHP 5.6, we can do this by implementing the new magic method __debugInfo() and returning an array with the debug information we want to expose:
If we now do a var_dump() of our File object, here’s the output we will get:
Much more useful, right?
The __debugInfo() magic method can be used for hiding information as well. Imagine the server is configured to dump into a log error information and classes that relate to an error. Or Xdebug is configured to dump stack traces and is showing object dumps in the browser. What about confidential information like connection strings or credentials? These will all be visible in plain sight! A potential disaster waiting to happen if this were in production… unless we hide the information we don’t want to be visible in these situations.
Here’s an example:
If we do a var_dump() of an instance of this class, we will never see the credit card number that is encapsulated in this class:
Develop with pleasure!
– JetBrains PhpStorm Team