PHP 7 will be the stricter PHP version, with over 400 error messages. They range from reporting a fatal compilation error to helping with strict standards. During the session, we'll review old-timer messages that are still plaguing us, new ones that are not linked to new features, and even a few that are disappearing. Funny at times, more serious at others, this session will help understand the messages that PHP reports and how to take advantage of them for better coding.
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(): array {
return false;
}
?>
Uncaught TypeError: Return value of x() must be of the type array,
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;
}
?>
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. 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
23. CANNOT DECLARE A RETURN
TYPE
• __construct
• __destruct
• __clone
• "PHP 4 constructor"
<?php
class x {
function __construct() : array {
return true;
}
function x() : array {
return true;
}
}
?>
24. 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;
}
}
?>
28. 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 rarely useful
<?php
class x {
const class = 1;
}
?>
29. 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 rarely useful
• 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)
30. DYNAMIC CLASS NAMES ARE NOT
ALLOWED IN COMPILE-TIME ::CLASS FETCH
<?php
$c = new class {
function f() {
echo $x::class;
}
};
$c->f();
?>
32. SWITCH STATEMENTS MAY ONLY
CONTAIN ONE DEFAULT CLAUSE
<?php
switch($x) {
case '1' :
break;
default :
break;
default :
break;
case '2' :
break;
}
?>
33. 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;
38. 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 REMOVE
39. 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
41. 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));
?>
42. DIVISION OF PHP_INT_MIN BY
-1 IS NOT AN INTEGER
• PHP_INT_MIN is the smallest integer on PHP
• PHP_INT_MAX : 9223372036854775807
• PHP_INT_MIN : -9223372036854775808
• Division or multiplication leads to non-integer
• Uses the Integer Division intdiv()
43. WEBP DECODE: REALLOC
FAILED
• New image format for the Web
• Lossless compression, small files
• gdImageCreateFromWebpCtx emit this
• Probably very bad
44. FUNCTION NAME MUST BE A
STRING
<?php
if ($_GET('X') == 'Go') {
ProcessFile();
return;
}
?>
47. 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
51. 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?
52. • die('I SEE ' . $action . ' - ' . $_POST['categories_id']);
• die("Error: application_top not found.nMake sure you have placed the
currency_cron.php 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);