Preparing for PHP 7 error messages at the blackboard

Preparing for PHP 7 error messages

Prepare for PHP 7 error messages

The first step to prepare for PHP 7 is to lint it : using the command line instruction ‘php -l script.php’, one can easily check that every file in a current application compile with PHP 7. The second step is to run the application and the unit tests : in short, execute it with PHP 7. And this is where we’ll learn about the new errors that PHP has prepared for us. In order to be one step ahead of the migration, this article will help you prepare here is a panorama on PHP error messages.

This graph shows the evolution of the number of error messages referenced in the C code of PHP.

 

 

PHPerrorMessageEvolution

Evolution of the number of error messages inside PHP

Blue line shows the evolution of the total number of errors that may be raised. We looked for ‘zend_error’ and ‘zend_error_noreturn’ function calls in the C code. The green line is the number of distinct error messages. For example, there are 192 occurrences where PHP raises an error called ‘Using $this when not in object context’. The 192 are counted 192 times in the blue graph, and counted once in the green one.

As you can see, there is a steady growth in the number of situations where PHP is checking the code and providing a human readable message. There is also a slight reduction in the diversity of errors available : sometimes, errors messages are made generic, such as ‘%s’ not in the ‘loop’ or ‘switch’ context’. This is a kind of error template, used to avoid using the same error for ‘break’ and ‘continue’, which may both be used in a loop or a switch.

Most often raised errors

Here is the top 7 of the most frequent errors in the PHP code. We also used Google to evaluate how frequent those errors were :

Error Occurrences in the code Occurences in using PHP
Using $this when not in object context 192 151 000
Cannot use string offset as an array 74 42 000
Cannot use string offset as an object 56 8
Only variable references should be yielded by reference 52 37
Undefined variable: %s 47 2 070 000
Attempt to increment/decrement property of non-object 40 1 600
Cannot use temporary expression in write context 33 37

All of them have been present in PHP for several versions : the most famous is indeed ‘Undefined variable’. The last one is the only one that was introduced in PHP 7, and the first result on Google is actually the RFC about ‘Uniform Variable Syntax’ (https://wiki.php.net/rfc/uniform_variable_syntax).

Uniform Variable Syntax makes a great job at making the variables syntax consistent in PHP 7. In particular, it makes PHP support some missing combinaisons, such as $foo()[‘bar’]() : this means calling a function stored in the ‘bar’ element of the returned value of the function $foo. It also makes possible the usage of nested ::, such as $foo::$bar::$baz.

The high number of potential error raised by UVS is probably linked to the actual checks that are now occuring. PHP 7 works hard to understand the situations, while PHP 5 only relied on a lazy ‘syntax error’ as feedback.

More errors messages for better clarity

The evolution of the number of error message shows that PHP is taking extra care of the consistency of the language, and help us fix any syntax mistake. Those extra error messages makes several situations clearer and even manageable : for example, PHP 7 has now Engine exceptions, that will be returned when compilation problems are found. This is highly needed for anyone running PHP deamons, that shouldn’t die just because someone pushed some incompilable code, but rather, handle the error nicely.

Stay tuned for the second part of this article : we’ll check a number of PHP7 error messages to get our code better prepared.