SlideShare a Scribd company logo
Preparing for the next 
PHP version
Towards PHP 5.5 and 5.6 
• Changing version is often a big challenge 
• Backward incompatibilities 
• New features 
• How to spot them ?
Speaker 
• Damien Seguy 
• CTO at exakat 
• Phather of the plush toy 
elePHPant 
• Will talk on automated 
code audit later
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 (5.6)
PHP lint will find 
• Short array syntax 
• Function subscripting 
• break/continue with variables 
• Rare other situations 
• Code that won’t compile anyway
Where else code will break? 
• PHP running has 3 stages 
• parsed 
• compiled 
• executed 
Checked with lint 
Checked code review 
Checked with data and UT
What will change? 
• Removed features 
• Deprecated features 
• Changed features 
• New features
Deprecated features 
• PHP 5.6 
• $HTTP_RAW_POST_DATA 
5.6 
• Call From Incompatible Context 
• iconv and mbstring directives go to default_charset 
• PHP 5.5 
• /e in preg_replace 
• ext/mysql 
5.5 
• mcrypt arguments
Deprecated features 
• $HTTP_RAW_POST_DATA 
• Replace it by php://input 
• php://input is now reusable 
• ext/mysql 
• Look for mysql_* functions 
• Probably in Db/Adapter 
5.6 
5.5
Search is your friend 
• Grep, or IDE’s search function will help you 
• Look for mysql_* 
• $HTTP_RAW_POST_DATA
Error_level to E_STRICT 
Deprecated: The mysql extension is 
deprecated and will be removed in 
the future: use mysqli or PDO 
instead in /path/to/filename.php on 
line 11
/e and charset directives 
• preg_replace(‘/ /e’, ‘evaled code’, 
$haystack) 
• replaced preg_replace_callback(‘/ /‘, 
closure, $haystack) 
• in php.ini, check for mbstring, iconv and 
default_charset 
5.5 
5.6
Where to look for 
• preg_replace 
• Search for preg_replace function calls 
• defaut_charset 
• Search for ini_set, ini_get, ini_get_all, 
ini_restore, get_cfg_var 
• Seach in php.ini, .htaccess
Incompatible context 
$ php53 test.php 
Notice: Undefined variable: this in test.php on line 3 
A 
<?php 
class A { 
function f() { echo get_class($this); } 
} 
A::f(); 
?> 
5.6 
$ php56 test.php 
Strict Standards: Non-static method A::f() should not be called 
statically in /Users/famille/Desktop/test.php on line 6 
Notice: Undefined variable: this in test.php on line 3 
A
Search for situations 
• Search for :: operator 
• Get the class 
• then the method 
• then the static keyword 
• Needs a automated auditing tool 
• Code sniffer, IDE
Strict Standards: Non-static method 
A::f() should not be called 
statically in test.php on line 6
Changed behavior 
• json_decode is stricter 
• it was more tolerant before with TRUE or False values 
• gmp resources are object 
• and not resources 
• search for is_resource() 
• mcrypt requires valid keys and vectors 
• check correct size and vector presence 
• pack and unpack 
• More compatible with Perl 
• Z and a format code must be checked 
5.6 
5.5
Added structures 
• PHP adds 
• constants 
• functions 
• extensions 
• traits 
• interfaces
Added structures 
Functions Classes Constants 
5.3 25 18 80 
5.4 0 9 78 
5.5 113 9 37 
5.6 19 0 24 
Total 157 36 219
New features 
• Fixing 
• Modernization 
• New feature
Fixing
empty() upgrade 
• No need 
anymore to 
expressions in a 
variable for 
empty()! 
function myFunction() { 
return -2 ; 
} 
if (empty(myFunction() + 2)) { 
echo "This means 0!n"; 
} 
Fatal error: Can't use function return value in write context in test.php on line 6 
5.5
SELF::const != self::const 
<?php 
class object { 
const x = 1; 
function x() { print SELF::x."n"; } 
} 
$object = new object(); 
$object->x(); 
?> 
Fatal error: Class 'SELF' not found in test.php on line 6 
5.5
Modernization
Dereferencing 
• Direct access to 
element in a string 
or an array. 
<?php 
/// Function dereferencing 
echo str_split("123", 1 )[2]; 
echo "n"; 
/// Array dereferencing 
echo [5, 5, 3][0]; 
echo "n"; 
/// String dereferencing 
echo 'PHP'[0]; 
echo "n"; 
5.5 ?>
Power operator 
• Replaces pow() 
• Be aware of precedence 
echo pow(2, 3); 
$a=2; 
$a **= 3; 
$a = 2 ** 3; 
5.6
… Variadic 
• replaces 
func_get_args() 
• Easier to read 
function array_power($pow, ...$integers) { 
foreach($integers as $i) { 
print "$i ^ $pow = ". ($i ** $pow)."n"; 
} 
} 
array_power(3, 1, 2, 3, 4, 5); 
1 ^ 3 = 1 
2 ^ 3 = 8 
3 ^ 3 = 27 
4 ^ 3 = 64 
5 ^ 3 = 125 
5.6
Variadic … 
• replaces 
call_user_func_array 
• Easier to read 
• Works on functions 
• Works with typehint 
• Doesn’t work with 
references 
function array_power($pow, ...$integers) { 
foreach($integers as $i) { 
print "$i ^ $pow = ". ($i ** $pow)."n"; 
} 
} 
array_power(3, ...range(1, 5)); 
array_power(3, ...[1, 2, 3, 4, 5]); 
array_power(3, ...[1, 2, 3], ...[4, 5]); 
1 ^ 3 = 1 
2 ^ 3 = 8 
3 ^ 3 = 27 
4 ^ 3 = 64 
5 ^ 3 = 125
Generators 
function factors($limit) { 
$r = array(2); 
for ($i = 3; $i <= $limit; $i += 2) { 
$r[] = $i; 
} 
return $r; 
} 
$prime = 135; 
foreach (factors(sqrt($prime)) as $n) { 
echo "$n ". ($prime % $n ? ' not ' : '') . " factor} 
• $step big => huge 
memory usage 
5.5
Generators 
• New yield keyword 
• Save memory from 
n down to 1 value 
• Good for long or 
infinite loops 
• Search for range(), 
for() or loops 
function factors($limit) { 
yield 2; 
for ($i = 3; $i <= $limit; $i += 2) { 
yield $i; 
} 
} 
$prime = 135; 
foreach (factors(sqrt($prime)) as $n) { 
echo "$n ". ($prime % $n ? ' not ' : '') . " factor}
function x() { 
$r = new resource(); 
Finally try { 
$result = $r->do(); 
} 
catch (NetworkException $e) { 
unset ($r); 
throw $e; 
} 
catch (UnexpectedException $e) { 
unset ($r); 
throw $e; 
} 
catch (DaylightSavingException $e) { 
unset ($r); 
throw $e; 
} 
unset ($r); 
return $result; 
} 
• Clean up after 
exception 
• What if 
return in try? 
• Move cleaning 
to 
__destruct()? 
5.5
Finally 
• Clean up after 
exception or 
not 
• Clean up even 
when return 
too early 
function x() { 
$r = new resource(); 
try { 
$result = $r->do(); 
} 
catch (NetworkException $e) { 
throw $e; 
} 
catch (UnexpectedException $e) { 
throw $e; 
} 
catch (DaylightSavingException $e) { 
// just ignore this one 
} finally { 
unset ($r) ; 
} 
return $result; 
}
Really new
Class name resolution 
<?php 
namespace NameSpace; 
class ClassName {} 
echo ClassName::class; 
echo "n"; 
?> 
• Get the full name of 
a class with ::class 
5.5
class somePasswordSa_fe {_ debugInfo() 
private $user; 
private $password; 
public function __construct($user, $password) { 
$this->user = $user; 
$this->password = $password; 
} 
public function __debugInfo() { 
return [ 
'user' => $this->password, 
'password' => '**********', 
]; 
} 
} 
print_r(new somePasswordSafe('root', 'secret')); 
somePasswordSafe Object 
( 
[user] => secret 
[password] => ********** 
) 
5.6
use const / functions 
namespace NameSpace { 
const FOO = 42; 
function f() { echo __FUNCTION__."n"; } 
} 
namespace { 
use const NameSpaceFOO; 
use function NameSpacef; 
echo FOO."n"; 
f(); 
} 
• Importing constants 
or functions from 
another namespace 
• Keep things 
separated 
• Avoid polluting 
global namespace 
• Avoid static only 
classes 
5.6
Constant scalar expressions 
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; 
public function f($a = (MAJOR == 2) ? 3 : Version::MINOR ** 3) { 
return $a; 
} 
} 
• Code automation 
• Won’t accept functioncalls 
• Keep it simple 
5.6
Foreach supports list 
• And any type 
<?php 
of keys 
• Good with 
Iterator 
classes 
• Not possible 
with Arrays 
class object implements Iterator { 
/.../ 
function key() { return array(1,2); } 
function current() { return 3; } 
/.../ 
} 
$object = new object(); 
foreach($object as list($a, $b) = $c) { 
print "$a + $b + $c = ".($a + $b + $c)."n"; 
} 
?> 
5.5
Context changes 
• PHP 5.6 
• Windows XP and 2003 dropped 
• Support for Zend Optimiser 
• PHP 5.5 
• phpdbg 
• Large File Upload 5.5 
5.6
www.slideshare 
.net/dseguy 
damien.seguy@gmail.com

More Related Content

What's hot (20)

PDF
SPL: The Missing Link in Development
jsmith92
 
ODP
PHP Tips for certification - OdW13
julien pauli
 
PDF
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
PPTX
Php string function
Ravi Bhadauria
 
PDF
Building Testable PHP Applications
chartjes
 
PDF
Just-In-Time Compiler in PHP 8
Nikita Popov
 
PPT
php 2 Function creating, calling, PHP built-in function
tumetr1
 
KEY
Workshop unittesting
Joshua Thijssen
 
PDF
Functions in PHP
Vineet Kumar Saini
 
PDF
What's new in PHP 8.0?
Nikita Popov
 
PPTX
Arrays &amp; functions in php
Ashish Chamoli
 
PDF
Static Optimization of PHP bytecode (PHPSC 2017)
Nikita Popov
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PPTX
A Functional Guide to Cat Herding with PHP Generators
Mark Baker
 
PDF
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
James Titcumb
 
PDF
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
PDF
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
PPTX
Php pattern matching
JIGAR MAKHIJA
 
PPTX
PHP 7
Joshua Copeland
 
PPT
Class 3 - PHP Functions
Ahmed Swilam
 
SPL: The Missing Link in Development
jsmith92
 
PHP Tips for certification - OdW13
julien pauli
 
PHP Enums - PHPCon Japan 2021
Ayesh Karunaratne
 
Php string function
Ravi Bhadauria
 
Building Testable PHP Applications
chartjes
 
Just-In-Time Compiler in PHP 8
Nikita Popov
 
php 2 Function creating, calling, PHP built-in function
tumetr1
 
Workshop unittesting
Joshua Thijssen
 
Functions in PHP
Vineet Kumar Saini
 
What's new in PHP 8.0?
Nikita Popov
 
Arrays &amp; functions in php
Ashish Chamoli
 
Static Optimization of PHP bytecode (PHPSC 2017)
Nikita Popov
 
Perl.Hacks.On.Vim
Lin Yo-An
 
A Functional Guide to Cat Herding with PHP Generators
Mark Baker
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
James Titcumb
 
関西PHP勉強会 php5.4つまみぐい
Hisateru Tanaka
 
Typed Properties and more: What's coming in PHP 7.4?
Nikita Popov
 
Php pattern matching
JIGAR MAKHIJA
 
Class 3 - PHP Functions
Ahmed Swilam
 

Similar to Preparing for the next PHP version (5.6) (20)

PDF
Php 5.6 From the Inside Out
Ferenc Kovács
 
ODP
Php in 2013 (Web-5 2013 conference)
julien pauli
 
ODP
PHP5.5 is Here
julien pauli
 
PDF
08 Advanced PHP #burningkeyboards
Denis Ristic
 
ODP
The why and how of moving to PHP 5.4/5.5
Wim Godden
 
PPTX
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
DrupalMumbai
 
PDF
PHP 5
Rafael Corral
 
ODP
The why and how of moving to php 5.4/5.5
Wim Godden
 
ODP
The why and how of moving to PHP 5.5/5.6
Wim Godden
 
PPTX
Migrating to PHP 7
John Coggeshall
 
PPTX
Introducing PHP Latest Updates
Iftekhar Eather
 
PPTX
Learning php 7
Ed Lomonaco
 
PDF
PHP 7.0 new features (and new interpreter)
Andrea Telatin
 
ODP
The why and how of moving to php 5.4
Wim Godden
 
PPTX
PHP7 Presentation
David Sanchez
 
PPTX
Php 5.4: New Language Features You Will Find Useful
David Engel
 
PDF
Preparing code for Php 7 workshop
Damien Seguy
 
PDF
Review unknown code with static analysis
Damien Seguy
 
PPTX
PHP 5.6 New and Deprecated Features
Mark Niebergall
 
ODP
What's new, what's hot in PHP 5.3
Jeremy Coates
 
Php 5.6 From the Inside Out
Ferenc Kovács
 
Php in 2013 (Web-5 2013 conference)
julien pauli
 
PHP5.5 is Here
julien pauli
 
08 Advanced PHP #burningkeyboards
Denis Ristic
 
The why and how of moving to PHP 5.4/5.5
Wim Godden
 
20 cool features that is in PHP 7, we missed in PHP 5. Let walkthrough with t...
DrupalMumbai
 
The why and how of moving to php 5.4/5.5
Wim Godden
 
The why and how of moving to PHP 5.5/5.6
Wim Godden
 
Migrating to PHP 7
John Coggeshall
 
Introducing PHP Latest Updates
Iftekhar Eather
 
Learning php 7
Ed Lomonaco
 
PHP 7.0 new features (and new interpreter)
Andrea Telatin
 
The why and how of moving to php 5.4
Wim Godden
 
PHP7 Presentation
David Sanchez
 
Php 5.4: New Language Features You Will Find Useful
David Engel
 
Preparing code for Php 7 workshop
Damien Seguy
 
Review unknown code with static analysis
Damien Seguy
 
PHP 5.6 New and Deprecated Features
Mark Niebergall
 
What's new, what's hot in PHP 5.3
Jeremy Coates
 
Ad

More from Damien Seguy (20)

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

Recently uploaded (20)

PDF
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
PDF
FME in Overdrive: Unleashing the Power of Parallel Processing
Safe Software
 
PPTX
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 
DoS Attack vs DDoS Attack_ The Silent Wars of the Internet.pdf
CyberPro Magazine
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
GDG Cloud Southlake #44: Eyal Bukchin: Tightening the Kubernetes Feedback Loo...
James Anderson
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Practical Applications of AI in Local Government
OnBoard
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Optimizing the trajectory of a wheel loader working in short loading cycles
Reno Filla
 
FME in Overdrive: Unleashing the Power of Parallel Processing
Safe Software
 
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Next Generation AI: Anticipatory Intelligence, Forecasting Inflection Points ...
dleka294658677
 

Preparing for the next PHP version (5.6)

  • 1. Preparing for the next PHP version
  • 2. Towards PHP 5.5 and 5.6 • Changing version is often a big challenge • Backward incompatibilities • New features • How to spot them ?
  • 3. Speaker • Damien Seguy • CTO at exakat • Phather of the plush toy elePHPant • Will talk on automated code audit later
  • 4. PHP linting • command line : php -l filename.php • Will only parse the code, • not execution • Will spot compilation problems
  • 6. PHP lint will find • Short array syntax • Function subscripting • break/continue with variables • Rare other situations • Code that won’t compile anyway
  • 7. Where else code will break? • PHP running has 3 stages • parsed • compiled • executed Checked with lint Checked code review Checked with data and UT
  • 8. What will change? • Removed features • Deprecated features • Changed features • New features
  • 9. Deprecated features • PHP 5.6 • $HTTP_RAW_POST_DATA 5.6 • Call From Incompatible Context • iconv and mbstring directives go to default_charset • PHP 5.5 • /e in preg_replace • ext/mysql 5.5 • mcrypt arguments
  • 10. Deprecated features • $HTTP_RAW_POST_DATA • Replace it by php://input • php://input is now reusable • ext/mysql • Look for mysql_* functions • Probably in Db/Adapter 5.6 5.5
  • 11. Search is your friend • Grep, or IDE’s search function will help you • Look for mysql_* • $HTTP_RAW_POST_DATA
  • 12. Error_level to E_STRICT Deprecated: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /path/to/filename.php on line 11
  • 13. /e and charset directives • preg_replace(‘/ /e’, ‘evaled code’, $haystack) • replaced preg_replace_callback(‘/ /‘, closure, $haystack) • in php.ini, check for mbstring, iconv and default_charset 5.5 5.6
  • 14. Where to look for • preg_replace • Search for preg_replace function calls • defaut_charset • Search for ini_set, ini_get, ini_get_all, ini_restore, get_cfg_var • Seach in php.ini, .htaccess
  • 15. Incompatible context $ php53 test.php Notice: Undefined variable: this in test.php on line 3 A <?php class A { function f() { echo get_class($this); } } A::f(); ?> 5.6 $ php56 test.php Strict Standards: Non-static method A::f() should not be called statically in /Users/famille/Desktop/test.php on line 6 Notice: Undefined variable: this in test.php on line 3 A
  • 16. Search for situations • Search for :: operator • Get the class • then the method • then the static keyword • Needs a automated auditing tool • Code sniffer, IDE
  • 17. Strict Standards: Non-static method A::f() should not be called statically in test.php on line 6
  • 18. Changed behavior • json_decode is stricter • it was more tolerant before with TRUE or False values • gmp resources are object • and not resources • search for is_resource() • mcrypt requires valid keys and vectors • check correct size and vector presence • pack and unpack • More compatible with Perl • Z and a format code must be checked 5.6 5.5
  • 19. Added structures • PHP adds • constants • functions • extensions • traits • interfaces
  • 20. Added structures Functions Classes Constants 5.3 25 18 80 5.4 0 9 78 5.5 113 9 37 5.6 19 0 24 Total 157 36 219
  • 21. New features • Fixing • Modernization • New feature
  • 23. empty() upgrade • No need anymore to expressions in a variable for empty()! function myFunction() { return -2 ; } if (empty(myFunction() + 2)) { echo "This means 0!n"; } Fatal error: Can't use function return value in write context in test.php on line 6 5.5
  • 24. SELF::const != self::const <?php class object { const x = 1; function x() { print SELF::x."n"; } } $object = new object(); $object->x(); ?> Fatal error: Class 'SELF' not found in test.php on line 6 5.5
  • 26. Dereferencing • Direct access to element in a string or an array. <?php /// Function dereferencing echo str_split("123", 1 )[2]; echo "n"; /// Array dereferencing echo [5, 5, 3][0]; echo "n"; /// String dereferencing echo 'PHP'[0]; echo "n"; 5.5 ?>
  • 27. Power operator • Replaces pow() • Be aware of precedence echo pow(2, 3); $a=2; $a **= 3; $a = 2 ** 3; 5.6
  • 28. … Variadic • replaces func_get_args() • Easier to read function array_power($pow, ...$integers) { foreach($integers as $i) { print "$i ^ $pow = ". ($i ** $pow)."n"; } } array_power(3, 1, 2, 3, 4, 5); 1 ^ 3 = 1 2 ^ 3 = 8 3 ^ 3 = 27 4 ^ 3 = 64 5 ^ 3 = 125 5.6
  • 29. Variadic … • replaces call_user_func_array • Easier to read • Works on functions • Works with typehint • Doesn’t work with references function array_power($pow, ...$integers) { foreach($integers as $i) { print "$i ^ $pow = ". ($i ** $pow)."n"; } } array_power(3, ...range(1, 5)); array_power(3, ...[1, 2, 3, 4, 5]); array_power(3, ...[1, 2, 3], ...[4, 5]); 1 ^ 3 = 1 2 ^ 3 = 8 3 ^ 3 = 27 4 ^ 3 = 64 5 ^ 3 = 125
  • 30. Generators function factors($limit) { $r = array(2); for ($i = 3; $i <= $limit; $i += 2) { $r[] = $i; } return $r; } $prime = 135; foreach (factors(sqrt($prime)) as $n) { echo "$n ". ($prime % $n ? ' not ' : '') . " factor} • $step big => huge memory usage 5.5
  • 31. Generators • New yield keyword • Save memory from n down to 1 value • Good for long or infinite loops • Search for range(), for() or loops function factors($limit) { yield 2; for ($i = 3; $i <= $limit; $i += 2) { yield $i; } } $prime = 135; foreach (factors(sqrt($prime)) as $n) { echo "$n ". ($prime % $n ? ' not ' : '') . " factor}
  • 32. function x() { $r = new resource(); Finally try { $result = $r->do(); } catch (NetworkException $e) { unset ($r); throw $e; } catch (UnexpectedException $e) { unset ($r); throw $e; } catch (DaylightSavingException $e) { unset ($r); throw $e; } unset ($r); return $result; } • Clean up after exception • What if return in try? • Move cleaning to __destruct()? 5.5
  • 33. Finally • Clean up after exception or not • Clean up even when return too early function x() { $r = new resource(); try { $result = $r->do(); } catch (NetworkException $e) { throw $e; } catch (UnexpectedException $e) { throw $e; } catch (DaylightSavingException $e) { // just ignore this one } finally { unset ($r) ; } return $result; }
  • 35. Class name resolution <?php namespace NameSpace; class ClassName {} echo ClassName::class; echo "n"; ?> • Get the full name of a class with ::class 5.5
  • 36. class somePasswordSa_fe {_ debugInfo() private $user; private $password; public function __construct($user, $password) { $this->user = $user; $this->password = $password; } public function __debugInfo() { return [ 'user' => $this->password, 'password' => '**********', ]; } } print_r(new somePasswordSafe('root', 'secret')); somePasswordSafe Object ( [user] => secret [password] => ********** ) 5.6
  • 37. use const / functions namespace NameSpace { const FOO = 42; function f() { echo __FUNCTION__."n"; } } namespace { use const NameSpaceFOO; use function NameSpacef; echo FOO."n"; f(); } • Importing constants or functions from another namespace • Keep things separated • Avoid polluting global namespace • Avoid static only classes 5.6
  • 38. Constant scalar expressions 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; public function f($a = (MAJOR == 2) ? 3 : Version::MINOR ** 3) { return $a; } } • Code automation • Won’t accept functioncalls • Keep it simple 5.6
  • 39. Foreach supports list • And any type <?php of keys • Good with Iterator classes • Not possible with Arrays class object implements Iterator { /.../ function key() { return array(1,2); } function current() { return 3; } /.../ } $object = new object(); foreach($object as list($a, $b) = $c) { print "$a + $b + $c = ".($a + $b + $c)."n"; } ?> 5.5
  • 40. Context changes • PHP 5.6 • Windows XP and 2003 dropped • Support for Zend Optimiser • PHP 5.5 • phpdbg • Large File Upload 5.5 5.6