PhpStorm 2017.1 Brings New PHP 7.x Support

PHP 7’s uniform variable syntax improvements were welcomed but opened up a whole new raft of problems for PHP developers to understand. PhpStorm 2017.1 brings full support for the uniform variable syntax changes, plus improved support for anonymous classes.

PHP 7’s uniform variable syntax is a difficult way of saying that PHP now treats the order in which you chain variables the same, no matter the place you do it. In the past, PHP has resolved variables differently depending on the context, but PHP 7 fixed this causing some PHP 5 code to break.  For example, let’s take the following code in PHP 5:

Running this code in PHP 5.6 would give you what you might expect:

Link is wielding a Wooden Mop

This is because PHP 5 resolves the $properties['weapon'] part first and then applies that to the property call of the $link object; effectively you are calling $link->meleeWeapon as the array lookup is done first.

When you run this code with PHP 7.1, you’ll see something different:

PHP Notice:  Array to string conversion in Link.php on line 12

PHP 7.1 errors, because the order in which this variable chain is processed has changed. Uniform variable syntax means that now, the order is evaluated left to right across the board, so PHP 7 first tries to evaluate $link->$properties and gives an error, because we cannot use a variable with an array type to lookup a class property. While this may be annoying in this instance, this behaviour happens everywhere, so variable evaluation is now predictable across the board.

In order to fix this code, we need to tell PHP 7 to evaluate the right-hand assignment before the left:

These evaluation changes also mean that you can now chain assignment calls in a way that wasn’t possible before, such as $function()['array_key']() which works if $function is a function that returns an array with the key array_key, which returns a function. As you can see, it can quickly get confusing as to what is actually happening in these assignments so personally, I still prefer to return to variables where possible to make my code more understandable.

While previous versions of PhpStorm have been aware of the uniform variable syntax changes, PhpStorm 2017.1 brings full support across your code.

This version of PhpStorm also comes with full support for PHP 7’s anonymous classes, so if you’re creating an anonymous class that extends or implements an existing class or interface, you can expect full code completion and inspections inside the anonymous class.

anon-classes-example

– Gary & The PhpStorm Team

About Gary Hockin

Gary Hockin has been creating code to power web applications for 15 years, the last three of them at an extraordinary level. He is handsome, talented, funny and articulate, and also extremely modest. Gary is a valued contributor to Zend Framework 2 and is a member of the community review team. He's married and has two wonderful children, and when he's not coding, writing about code, speaking about code or reading about code, he can usually be found in the pub playing pool.
This entry was posted in Cool Feature, Newsletter. Bookmark the permalink.
  • Haehnchen

    Code folding for anonymous classes like in JAVA would also be nice :)

    • Vladimir Luchansky

      Feel free to submit the feature request to our tracking system here: . :-)

    • https://www.clippp.com/susangriffin/beckham-tattoo Beckham Tattoo

      This is because PHP 5 resolves the $properties[‘weapon’] part first and then applies that to the property call of the $link object; effectively you are calling $link->meleeWeapon as the array lookup is done first.