SlideShare a Scribd company logo
MIGRATING TO
NEW PHP VERSIONS
Washington DC, USA
TOWARDS PHP 70
Changing version is always a big challenge
Backward incompatibilities
New features
How to spot them ?
SPEAKER
Damien Seguy
CTO at exakat
Static code analysis for PHP
PHP LINTING
command line : php -l filename.php
Will only parse the code,
not execution
Will spot compilation problems
Preparing for the next php version
Preparing for the next php version
Preparing for the next php version
PHP -L WILL FIND
Short array syntax
Function subscripting
Code that won’t compile anyway
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)
WHERE ELSE CODE WILL BREAK?
PHP running has 3 stages
parsed
compiled
executed
Checked with lint
Checked with data and UT
Checked code review
GETTING READY
http://php.net/manual/en/migration70.php
UPGRADING TO PHP 7, Davey Shafik
https://github.com/php/php-src/blob/master/
UPGRADING
https://github.com/php/php-src/blob/master/NEWS
get_headers() has an extra parameter in 7.1
WHAT WILL CHANGE?
Incompatible changes
Deprecated changes
Changed features
New features
INCOMPATIBILITIES
Features that were dropped
Features that were added
ADDED STRUCTURES
Functions Classes Constants
5.3 40 2 80
5.4 0 9 78
5.5 12 11 57
5.6 1 10 10
7.0 10 10 41
Total 1293 153 1149
NAME IMPACT
get_resources(), intdiv()
PREG_JIT_STACKLIMIT_ERROR
Error, Date
REMOVED FEATURES
$HTTP_RAW_POST_DATA
Replace it by php://input
php://input is now reusable
REMOVED FEATURES
ext/mysql
Look for mysql_* functions
Probably in Db/Adapter
ext/ereg
ereg, ereg_replace, split, sql_regcase
USORT
<?php
$array = array(
    'foo',
    'bar',
    'php'
);
usort($array, function($a, $b) {
    return 0;
}
);
print_r($array);
Array
(
[0] => php
[1] => bar
[2] => foo
)
Array
(
[0] => foo
[1] => bar
[2] => php
)
PHP 5
PHP 7
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
PREG_REPLACE AND /E
preg_replace(‘/ /e’, ‘evaled code’, $haystack)
replaced preg_replace_callback(‘/ /‘, closure, $haystack)
preg_replace_callback_array()
PREG_REPLACE_CALLBACK_ARRAY
<?php 
$code = "abbbb";
$spec = 'c';
echo preg_replace_callback_array(
    array(
        "/a/" => function($matches) {
                        return strtoupper($matches[0]);
                 },
        "/b/" => function($matches) use ($spec) {
static $i = 0;
$i++;
               return "B$i$spec";
        }
    ), $code);
AB1cB2cB3cB4c
DEFAULT_CHARSET
iconv.input_encoding
iconv.output_encoding
iconv.internal_encoding
mbstring.http_input
mbstring.http_output
mbstring.internal_encoding
default_charset
DEFAULT_CHARSET
htmlentities()
PHP 5.3 : ISO-8859-1
PHP 5.4 : UTF-8
PHP 5.6 : default_charset (also UTF 8)
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()
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
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
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
WHERE TO LOOK FOR ?
Use error level
Set error_level to maximum
Spot errors in the log
Refine
Great to reduce log size
INCOMPATIBLE CONTEXT
<?php 
class A { 
     function f() { echo get_class($this); } 
} 
A::f(); 
?>
Notice: Undefined variable: this in
A
Deprecated: Non-static method A::f() should not be called statically in
Notice: Undefined variable: this in
A
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
CHANGED BEHAVIOR
Indirect expressions
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
STATIC ANALYZIS
PHP 5, PHP 7
Psr-4
ClearPHP
Performance
 
 

SUMMARY
PHP lint is your friend
Search in the code
With Grep
Directly, or indirectly
With the logs
Use static analysis tools
NEW FEATURES
They require willpower
Breaks backward compatibility
FUD
Search for places to apply them like for
incompatibilities
NEW FEATURES
Fixing
Modernization
New feature
FIXING
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"; 
} 
?>
MODERNIZATION
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
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';
?>
DIRNAME() SECOND ARG
<?php  
$path = '/a/b/c/d/e/f';
// PHP 5.6
$root = dirname(dirname(dirname($x)));
// PHP 7
$root = dirname($path, 3);
?>
… VARIADIC
replaces func_get_args()
Easier to read
<?php 
// PHP 5.5
function array_power($pow) {  
   $integers = func_get_args();
   array_unshift($integers);
   foreach($integers as $i) {  
      print "$i ^ $pow  = ". pow($i, $pow)."n";
   }  
}  
   
// PHP 7.0
function array_power($pow, ...$integers) {  
   foreach($integers as $i) {  
      print "$i ^ $pow  = ". ($i ** $pow)."n"; 
   }  
5.6
VARIADIC …
<?php 
// Avoid! 
foreach($source as $x) {
  if (is_array($x))
     $final = array_merge($final, $x);
  }
}
VARIADIC …
<?php 
$collection = [];
foreach($source as $x) {
  if (is_array($x))
     $collection[] = $x;
  }
}
// PHP 5.5
$final = call_user_func_array('array_merge', $collection);
   
// PHP 7.0
$final = array_merge(...$collection);
REALLY NEW
SCALAR TYPE TYPEHINT
Whenever type is tested
<?php  
function foo(string $x) {
   if (!is_string($x)) {
     throw new Exception('Type error while calling '.__FUNCTION__);
   }
}
GENERATORS
<?php  
function factors($limit) { 
    yield 2; 
    yield 3;
    yield from prime_database();
    for ($i = 1001; $i <= $limit; $i += 2) { 
        yield $i; 
    } 
} 
$prime = 1357; 
foreach (factors(sqrt($prime)) as $n) { 
    echo "$n ". ($prime % $n ? ' not ' : '') . " factorn"; 
}
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
<?php  
class Version { 
    const MAJOR = 2; 
    const MIDDLE = ONE; 
    const MINOR = 1; 
    const FULL = Version::MAJOR.'.'.Version::MIDDLE.'.'.Version::MINOR.
'-'.PHP_VERSION; 
    const SHORT = Version::MAJOR.'.'.Version::MIDDLE; 
    const COMPACT = Version::MAJOR.Version::MIDDLE.Version::MINOR; 
    const AN_ARRAY = [1,2,3,4];
    public function f($a = (MAJOR == 2) ? 3 : Version::MINOR ** 3) { 
        return $a; 
    } 
}
CONSTANT SCALAR EXPRESSIONS
Code automation
Keep it simple
Won’t accept functioncalls
Won't accept variables
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]);
    } 
}
SUMMARY
Check the manuals
PHP lint is your friend
Search in the code
Use static analysis tools
THANK YOU!
damien.seguy@gmail.com
http://joind.in/talk/view/14770

More Related Content

PPTX
Migrating to PHP 7
PPTX
PHP 5.3
ODP
Mastering Namespaces in PHP
PDF
PHP traits, treat or threat?
PPTX
ODP
Incredible Machine with Pipelines and Generators
ODP
Object Oriented Design Patterns for PHP
PDF
Php 7.2 compliance workshop php benelux
Migrating to PHP 7
PHP 5.3
Mastering Namespaces in PHP
PHP traits, treat or threat?
Incredible Machine with Pipelines and Generators
Object Oriented Design Patterns for PHP
Php 7.2 compliance workshop php benelux

What's hot (20)

PPTX
PLSQL Coding Guidelines - Part 6
PDF
2021.laravelconf.tw.slides2
PPTX
PL/SQL & SQL CODING GUIDELINES – Part 7
PPTX
PHP slides
PDF
Php exceptions
PPT
PPTX
Php.ppt
PPTX
PPTX
Php string function
PDF
Static Analysis of PHP Code – IPC Berlin 2016
PPTX
Listen afup 2010
PPT
Introduction to PHP
PDF
How to write maintainable code without tests
PDF
Continuous Quality Assurance
ODP
The why and how of moving to php 5.4
PPT
PHP - Introduction to PHP Error Handling
PPT
PPTX
Handling error & exception in php
PPT
PHP - Introduction to PHP - Mazenet Solution
PDF
Trying to learn C# (NDC Oslo 2019)
PLSQL Coding Guidelines - Part 6
2021.laravelconf.tw.slides2
PL/SQL & SQL CODING GUIDELINES – Part 7
PHP slides
Php exceptions
Php.ppt
Php string function
Static Analysis of PHP Code – IPC Berlin 2016
Listen afup 2010
Introduction to PHP
How to write maintainable code without tests
Continuous Quality Assurance
The why and how of moving to php 5.4
PHP - Introduction to PHP Error Handling
Handling error & exception in php
PHP - Introduction to PHP - Mazenet Solution
Trying to learn C# (NDC Oslo 2019)
Ad

Viewers also liked (15)

PPT
Wikis
PPT
Ego For You
PDF
Smartphones & Mobile Internet
DOCX
Biblioteca virtual
PDF
MANJUSHREE PALIT CV_April 2016
PPTX
Comercio internacional
PPTX
Veterinaria
PDF
OnlineTyari Case Study
PDF
Kesha Skirnevskiy, Zebrainy
PDF
Alexander Lukin, Yandex
PDF
Eugene Krasichkov, Intel
PDF
Andrey Zimenko, WG Labs
PDF
Matthew Wilson, Director of Business Development, Rovio Entertainment
PDF
Hunt for dead code
PPT
Huerta de las Pilas - Acuifero english version
Wikis
Ego For You
Smartphones & Mobile Internet
Biblioteca virtual
MANJUSHREE PALIT CV_April 2016
Comercio internacional
Veterinaria
OnlineTyari Case Study
Kesha Skirnevskiy, Zebrainy
Alexander Lukin, Yandex
Eugene Krasichkov, Intel
Andrey Zimenko, WG Labs
Matthew Wilson, Director of Business Development, Rovio Entertainment
Hunt for dead code
Huerta de las Pilas - Acuifero english version
Ad

Similar to Preparing for the next php version (20)

PDF
Php 7 compliance workshop singapore
PDF
Preparing code for Php 7 workshop
PDF
Last train to php 7
PPTX
Learning php 7
PDF
The why and how of moving to php 8
PPTX
Peek at PHP 7
PDF
The why and how of moving to php 7
ODP
Is your code ready for PHP 7 ?
ODP
The why and how of moving to php 7.x
ODP
The why and how of moving to php 7.x
PDF
Preparing for the next PHP version (5.6)
PDF
Review unknown code with static analysis
PDF
Damien seguy php 5.6
PPTX
PHP7 Presentation
PDF
Start using PHP 7
PDF
What To Expect From PHP7
PDF
Php 7 errors messages
PDF
Php 5.6 From the Inside Out
PPTX
PHP 7 Crash Course
PPTX
New in php 7
Php 7 compliance workshop singapore
Preparing code for Php 7 workshop
Last train to php 7
Learning php 7
The why and how of moving to php 8
Peek at PHP 7
The why and how of moving to php 7
Is your code ready for PHP 7 ?
The why and how of moving to php 7.x
The why and how of moving to php 7.x
Preparing for the next PHP version (5.6)
Review unknown code with static analysis
Damien seguy php 5.6
PHP7 Presentation
Start using PHP 7
What To Expect From PHP7
Php 7 errors messages
Php 5.6 From the Inside Out
PHP 7 Crash Course
New in php 7

More from Damien Seguy (20)

PDF
Strong typing @ php leeds
PPTX
Strong typing : adoption, adaptation and organisation
PDF
Qui a laissé son mot de passe dans le code
PDF
Analyse statique et applications
PDF
Top 10 pieges php afup limoges
PDF
Top 10 php classic traps DPC 2020
PDF
Meilleur du typage fort (AFUP Day, 2020)
PDF
Top 10 php classic traps confoo
PDF
Tout pour se préparer à PHP 7.4
PDF
Top 10 php classic traps php serbia
PDF
Top 10 php classic traps
PDF
Top 10 chausse trappes
PDF
Code review workshop
PDF
Understanding static analysis php amsterdam 2018
PDF
Review unknown code with static analysis php ce 2018
PDF
Everything new with PHP 7.3
PDF
Php 7.3 et ses RFC (AFUP Toulouse)
PDF
Tout sur PHP 7.3 et ses RFC
PDF
Review unknown code with static analysis php ipc 2018
PDF
Code review for busy people
Strong typing @ php leeds
Strong typing : adoption, adaptation and organisation
Qui a laissé son mot de passe dans le code
Analyse statique et applications
Top 10 pieges php afup limoges
Top 10 php classic traps DPC 2020
Meilleur du typage fort (AFUP Day, 2020)
Top 10 php classic traps confoo
Tout pour se préparer à PHP 7.4
Top 10 php classic traps php serbia
Top 10 php classic traps
Top 10 chausse trappes
Code review workshop
Understanding static analysis php amsterdam 2018
Review unknown code with static analysis php ce 2018
Everything new with PHP 7.3
Php 7.3 et ses RFC (AFUP Toulouse)
Tout sur PHP 7.3 et ses RFC
Review unknown code with static analysis php ipc 2018
Code review for busy people

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Cloud computing and distributed systems.
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Programs and apps: productivity, graphics, security and other tools
Diabetes mellitus diagnosis method based random forest with bat algorithm
The AUB Centre for AI in Media Proposal.docx
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Reach Out and Touch Someone: Haptics and Empathic Computing
Building Integrated photovoltaic BIPV_UPV.pdf
Cloud computing and distributed systems.
sap open course for s4hana steps from ECC to s4
Review of recent advances in non-invasive hemoglobin estimation
Network Security Unit 5.pdf for BCA BBA.
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Preparing for the next php version