This document discusses PHP 7.0's error messages. It begins with an agenda to review 2200 error messages and discuss new features, traps, and messages. The speaker is identified as the CTO of exakat, a PHP static auditing engine. The top discusses evolving error messages by searching code repositories and outlines the top 5 error messages found in the source code and via Google searches. It then outlines new error messages, better linting, removed messages, and cryptic messages.
6. TOP 5 (from the source)
1. Using $this when not in object context (192)
2. Cannot use string offset as an array (74)
3. Cannot use string offset as an object (56)
4. Only variable references should be yielded by
reference (52)
5. Undefined variable: %s (43)
7. TOP 5 (Google)
1. Call to undefined function
2. Class not found
3. Allowed memory size of
4. Undefined index
5. Undefined variable
11. Return value of %s%s%s()
must %s%s, %s%s returned
<?php
function x(): myClass {
return false;
}
?>
Uncaught TypeError: Return value of x() must be of the type myClass,
boolean returned in…
12. Return value of %s%s%s()
must %s%s, %s%s returned
<?php
function x(): array {
return ;
}
?>
Uncaught TypeError: Return value of x() must be of the type array,
none returned in…
13. Argument %d passed to %s%s%s() must %s
%s, %s%s given, called in %s on line %d
<?php
function x(array $a) {
return false;
}
x(false);
?>
Uncaught TypeError: Argument 1 passed to x() must be of the type
array, boolean given, called in
14. Default value for parameters with
a float type hint can only be float
<?php
function foo(float $a = "3"){
return true;
}
?>
15. Default value for parameters with
a float type hint can only be float
<?php
function foo(float $a = 3 ){
return true;
}
?>
16. Cannot use temporary
expression in write context
<?php
$a = 'foo'[0];
'foo'[0] = 'b';
?>
Parse error: syntax error, unexpected '=' in
18. Cannot use "%s" when no
class scope is active
<?php
function x(): parent {
return new bar();
}
x();
?>
• self • parent • static
19. Cannot use "%s" when no
class scope is active
<?php
class bar extends baz {}
class foo extends bar {
function x(): parent {
return new foo();
}
}
$x = new foo(); $x->x();
?>
20. Cannot use "%s" when no
class scope is active
<?php
class bar extends baz {}
class foo extends bar {
function x(): parent {
return new bar();
}
}
$x = new foo(); $x->x();
?>
21. Cannot use "%s" when no
class scope is active
<?php
class baz {}
class bar extends baz {}
class foo extends bar {
function x(): parent {
return new baz();
}
}
$x = new foo(); $x->x();
?>Uncaught TypeError: Return value of foo::x() must be an instance of
bar, instance of baz returned in
22. Different parents
class great-great-grandparent
class great-grandparent
class parent
class self
class child
class grand-child
class great-grandchild
class great-great-grandchild
<?php
class self {
function x() {
parent::y();
}
}
<?php
class self {
function x() : parent {
return $something;
}
}
23. Cannot use "%s" when no
class scope is active<?php
class baz {}
class bar extends baz {}
class foo extends bar {
function x(): grandparent {
return new baz();
}
}
$x = new foo(); $x->x();
?>
Uncaught TypeError: Return value of foo::x() must be an instance of
grandparent, instance of bar returned
24. Useful in traits
Fatal error: Cannot access parent:: when current
class scope has no parent
<?php
trait t {
function x() : parent {
return $something;
}
}
class c {
use t;
}
25. cannot declare a return type
• __construct
• __destruct
• __clone
• "PHP 4 constructor"
<?php
class x {
function __construct() : array {
return true;
}
function x() : array {
return true;
}
}
?>
26. Methods with the same name as their
class will not be constructors in a
future version of PHP; %s has a
deprecated constructor
<?php
class x {
function x() : array {
return true;
}
}
?> M
ost
frequent
error
(95%
)
30. A class constant must not be called 'class';
it is reserved for class name fetching
• Used to be a parse error. Now a nice message.
• Still a bad idea
<?php
class x {
const class = 1;
const const = 2;
}
?>
31. A class constant must not be called 'class';
it is reserved for class name fetching
• Outside class will
generate
the old error
<?php
//class x {
const class = 1;
//}
?>
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting
identifier (T_STRING)
32. Dynamic class names are not
allowed in compile-time ::class fetch
<?php
$c = new class {
function f() {
echo $x::class;
}
};
$c->f();
?>
34. Switch statements may only
contain one default clause
<?php
switch($x) {
case '1' :
break;
default :
break;
default :
break;
case '2' :
break;
}
?>
35. Switch statements may only
contain one default clause
<?php
switch($x) {
case 1 :
break;
case 0+1 :
break;
case '1' :
break;
case true :
break;
case 1.0 :
break;
case $y :
break;
39. Gone for good
It is not safe to rely on the system's timezone
settings. You are *required* to use the date.timezone
setting or the date_default_timezone_set() function. In
case you used any of those methods and you are still
getting this warning, you most likely misspelled the
timezone identifier.
44. Call-time pass-by-reference
has been removed;
<?php
$a = 3;
function f($b) {
$b++;
}
f(&$a);
print $a;
?>
Fatal error: Call-time pass-by-reference has been removed; If you would
like to pass argument by reference, modify the declaration of f(). in
has been removed
45. Call-time pass-by-reference
has been removed;
<?php
$a = 3;
function f($b) {
$b++;
}
f(&$a);
print $a;
?>
PHP Parse error: syntax error, unexpected '&' in
47. Minimum value must be less than
or equal to the maximum value
<?php
var_dump(random_int(100, 999));
var_dump(random_int(-1000, 0));
var_dump(random_bytes(10));
?>
PHP Parse error: Could not gather sufficient random data
48. Division of PHP_INT_MIN
by -1 is not an integer
• PHP_INT_MAX : 9223372036854775807
• PHP_INT_MIN is the smallest integer on PHP
• PHP_INT_MIN : -9223372036854775808
• Division or multiplication leads to non-integer
• Uses the Integer Division intdiv()
49. WebP decode: realloc failed
• New image format for the Web
• Lossless compression, small files
• gdImageCreateFromWebpCtx emit this
• Probably very bad
50. Function name must be
a string
<?php
if ($_GET('X') == 'Go') {
ProcessFile();
return;
}
?>
53. Namespace declaration
statement
has to be
the very first statement
in the script
Encoding declaration pragma
must be
the very first statement
in the script
58. Fun with errors
• Check the errors messages in your application
• die, exit
• echo, print, display, debug, wp_die
(depends on conventions)
• new *Exception()
• What does your application tells you?
59. • die('I SEE ' . $action . ' - ' . $_POST['categories_id']);
• die("Error: application_top not found.nMake sure you have placed the currency_cron.ph
file in your (renamed) Admin folder.nn");
• die('ERROR: admin/includes/configure.php file not found. Suggest running zc_install/
index.php?');
• die('I WOULD NOT ADD ' . $new_categories_sort_array[$i] . '<br>');
• die('NOTCONFIGURED');
• die('halted');
• die('<pre>' . print_r($inputs, true));
• die('HERE_BE_MONSTERS - could not open file');}
• die('HERE_BE_MONSTERS');}
• die($prod_id);
• die('here');
• die('Sorry. File not found. Please contact the webmaster to report this error.<br />c/
f: ' . $origin_filename);