While many systems are still supporting PHP 5.3, we are rushing PHP 5.6 and even PHP 7. To ensure a good migration, one must go beyond checking for compilation: the code must be reviewed to avoid pitfalls like obsoletes functions, changes in default parameters or behavior, and backward incompatibilities. It must also start taking advantage of new features and drop old habits. We'll review a checklist of the common traps and help your code be PHP 7 compatible
4. PHP LINTING
command line : php -l filename.php
Will only parse the code,
not execution
Will spot compilation problems
5.
6.
7.
8. PHP -L WILL FIND
Short array syntax
Function subscripting
Code that won’t compile anyway
9. PHP 7 LINTING
Methods with the same name as their class will not be
constructors in a future version of PHP
Cannot use abcInt as Int because 'Int' is a special class
name
Switch statements may only contain one default clause
Redefinition of parameter $%s
syntax error, unexpected 'new' (T_NEW)
10. WHERE ELSE CODE WILL BREAK?
PHP running has 3 stages
parsed
compiled
executed
Checked with lint
Checked with data and UT
Checked code review
19. WHERE TO LOOK FOR ?
Find the name of the structure (function name…)
Grep, or IDE’s search function will help you
$HTTP_RAW_POST_DATA
Look for mysql_*
Look ereg, split, usort
24. WHERE TO LOOK FOR ?
preg_replace
Search for preg_replace function calls
Refine with /e, multiples calls
default_charset
Search for ini_set, ini_get, ini_get_all, ini_restore, get_cfg_var
Seach in php.ini, .htaccess
Search for htmlentities(), html_entity_decode() and htmlspecialchars()
25. DEPRECATED FEATURES
Methods with the same name as their
class will not be constructors in a
future version of PHP; foo has a
deprecated constructor
Not happening if a parent case has a __constructor()
Not happening if the class is in a namespace
26. Deprecated: Methods with the same name as their class
will not be constructors in a future version of PHP;
foo has a deprecated constructor
PHP 4 CONSTRUCTORS
Use the E_DEPRECATED error level while in DEV
Check the logs
27. CALL-TIME PASS-BY-REFERENCE
References are in the function signature
Deprecated warnings until PHP 7
A nice Parse error in PHP 7
<?php
$a = 3;
function f($b) {
$b++;
}
f(&$a);
print $a;
?>
PHP Parse error: syntax error, unexpected '&' in
28. WHERE TO LOOK FOR ?
Use error level
Set error_level to maximum
Spot errors in the log
Refine
Great to reduce log size
30. EASY TO SPOT
Use the E_DEPRECATED or strict while in DEV
Strict Standards: Non-static method A::f() should not
be called statically in test.php on line 6
Deprecated: Non-static method A::f() should not be
called statically in test.php on line 6
32. SEARCH FOR SITUATIONS
Search for :: operator
Get the class
then the method
then the static keyword
Needs a automated auditing tool
Exakat, Code sniffer, IDE
38. EMPTY() UPGRADE
No need anymore to expressions in a variable for empty()!
Fatal error: Can't use function return value in write context in test.php on line 6
5.5
<?php
function myFunction() {
return -2 ;
}
if (empty(myFunction() + 2)) {
echo "This means 0!n";
}
?>
40. SPACESHIP OPERATOR
Replaces a lot of code
Mainly useful for usort and co
Very Cute
<?php
// PHP 5.6
if ($a > $b) {
echo 1;
} elseif ($a < $b) {
echo -1;
} else {
echo 0;
}
// PHP 7.0
echo $a <=> $b; // 0
41. NULL-COALESCE
Shorter way to give a test for NULL and failover
<?php
// PHP 5.6
$x = $_GET['x'] === null ? 'default' : $_GET['x'];
// PHP 7.0
$x = $_GET['x'] ?? 'default';
?>
47. SCALAR TYPE TYPEHINT
Whenever type is tested
<?php
function foo(string $x) {
if (!is_string($x)) {
throw new Exception('Type error while calling '.__FUNCTION__);
}
}
49. GENERATORS
New yield keyword
Save memory from
n down to 1 value
Good for long or infinite loops
Search for range(), for() or loops
literals, database result sets, file lines
51. CONSTANT SCALAR EXPRESSIONS
Lots of properties should be constants
<?php
class Version {
const SUPPORTED = ['1.0', '1.1', '2.0', '2.1'];
private $an_array = [1,2,3,4];
public function isSupported($x) {
return isset(Version::SUPPORTED[$x]);
}
}