PHP 5.6 what’s new? 
Zoetemer, Nederlands, November 7th 2014 
Damien Seguy
What’s new in PHP 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 
• PHP static auditing
What will change? 
• Removed features 
• Deprecated features 
• Changed features 
• New features
Deprecated features 
• PHP 5.6 
• Array keys won’t be overwritten 
• $HTTP_RAW_POST_DATA is gone 
• No more Calls From Incompatible Context 
• iconv and mbstring directives go to default_charset 
• gmp resources are objects 
• mcrypt now needs a valid key
Deprecated 
Deprecated: The mysql extension is 
deprecated and will be removed in 
• Not removed yet! 
• ext/mysql was deprecated in 5.5 
the future: use mysqli or PDO 
instead in /path/to/filename.php on 
• It is still in the code! 
line 11 
• Will generate errors when using it 
• Set your error_reporting to E_STRICT when coding!
Array keys won’t be 
overwritten 
<?php 
class C { 
const ONE = 1; 
public $array = [ 
self::ONE => 'foo', 
'bar', 
'quux', 
]; 
} 
var_dump((new C)->array); 
?> 
array(2) { 
[0]=> 
string(3) "bar" 
[1]=> 
string(4) "quux" 
} 
array(3) { 
[1]=> 
string(3) "foo" 
[2]=> 
string(3) "bar" 
[3]=> 
string(4) "quux" 
} 
PHP 5.5 
PHP 5.6
$HTTP_RAW_POST_DATA 
• $HTTP_RAW_POST_DATA is gone 
• always_populate_raw_post_data = -1 
• Replace it by file_get_contents(php://input) 
• SOAP, JSON, AMP, any protocol specific 
Web 
server 
$HTTP_RAW_POST_DATA 
$_POST 
Script
charset directives 
iconv.input_encoding = ISO-8859-1 
iconv.internal_encoding = ISO-8859-1 
iconv.output_encoding = ISO-8859-1 
mbstring.internal_encoding = UTF-8 
mbstring.http_input = UTF-8 
mbstring.http_output = pass 
default_charset 
• in php.ini, check for mbstring, iconv directives are 
replace by default_charset
Check in your code 
• defaut_charset 
• Search for ini_set, ini_get, ini_get_all, ini_restore, 
get_cfg_var 
• Seach in php.ini, .htaccess
htmlentities 
• PHP 5.3 : htmlentities uses iso-8859-1 
• PHP 5.4 : htmlentities uses UTF-8 
• PHP 5.6 : htmlentities uses default_charset 
• default_charset uses UTF-8 
• htmlentities, htmlhtml_entity_decode and 
htmlspecialchars should have default values specified
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(); 
?> 
$ 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
Easy to spot 
Strict Standards: Non-static method A::f() should 
not be called statically in test.php on line 6 
Use the E_DEPRECATED while in DEV 
Keep updated
Changed behavior 
• json_decode is stricter 
• it was more tolerant before with TRUE or False values 
• gmp resources are object 
• and not resources (is_resource()) 
• mcrypt requires valid keys and vectors 
• check correct size and vector presence
Upgraded versions 
• PRCE : 8.34 
• oniguruma 5.9.5 
• libmagic : 5.17 
• Fixed 180+ bugs 
• http://php.net/ChangeLog-5.php
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
Modernization
Power operator 
• Replaces pow() 
• Be aware of precedence 
$a = pow(2, 3); 
$a = 2; 
$a **= 3; 
$a = 2 ** 3; 
$z = 1 * 2 ** 3 + 4 ** 5 ;
… 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
Variadic … 
• replaces 
call_user_func_array 
• Easier to read 
• Works on functions 
• Works with typehint 
• Doesn’t work with 
references or default 
values 
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
Really new
__debugInfo() 
somePasswordSafe Object 
( 
[user] => secret 
[password] => ********** 
) 
class somePasswordSafe { 
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'));
use const / functions 
• Importing constants or 
functions from another 
namespace 
• Keep things separated 
• Avoid polluting global 
namespace 
• Avoid static only classes 
namespace NameSpace { 
const FOO = 42; 
function f() { echo __FUNCTION__."n"; } 
} 
namespace { 
use const NameSpaceFOO; 
use function NameSpacef; 
echo FOO."n"; 
f(); 
}
Constant scalar expressions 
<?php 
class Version { 
const MAJOR = 2; 
const MIDDLE = ONE; 
const MINOR = 1; 
const FULL = Version::MAJOR.'.'.Version::MIDDLE.'.'.Version::MINOR.'-'._VERSION; 
static $SHORT = null; 
function __construct() { 
self::$SHORT = Version::MAJOR.'.'.Version::MIDDLE; 
} 
} 
?>
Constant scalar expressions 
• Code automation 
• Won’t accept functioncalls, variables, structures 
• Keep it simple
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; 
• Code automation 
• Won’t accept functioncalls, variables 
• Keep it simple 
public function f($a = (Version::MAJOR == 2) ? 3 : Version::MINOR ** 3) { 
return $a; 
} 
}
Context changes 
• PHP 5.6 
• Windows XP and 2003 dropped 
• Support for Zend Optimiser 
• Uploads over 2 Gb
Strategies for upgrading? 
• Lint your code 
• Probably won’t find much 
• Review the slides 
• Make sure your frameworks and libraries are ready too
www.slideshare. 
net/dseguy 
damien.seguy@gmail.com

Damien seguy php 5.6

  • 1.
    PHP 5.6 what’snew? Zoetemer, Nederlands, November 7th 2014 Damien Seguy
  • 2.
    What’s new inPHP 5.6? • Changing version is often a big challenge • Backward incompatibilities • New features • How to spot them ?
  • 3.
    Speaker • DamienSeguy • CTO at exakat • Phather of the plush toy elePHPant • PHP static auditing
  • 4.
    What will change? • Removed features • Deprecated features • Changed features • New features
  • 5.
    Deprecated features •PHP 5.6 • Array keys won’t be overwritten • $HTTP_RAW_POST_DATA is gone • No more Calls From Incompatible Context • iconv and mbstring directives go to default_charset • gmp resources are objects • mcrypt now needs a valid key
  • 6.
    Deprecated Deprecated: Themysql extension is deprecated and will be removed in • Not removed yet! • ext/mysql was deprecated in 5.5 the future: use mysqli or PDO instead in /path/to/filename.php on • It is still in the code! line 11 • Will generate errors when using it • Set your error_reporting to E_STRICT when coding!
  • 7.
    Array keys won’tbe overwritten <?php class C { const ONE = 1; public $array = [ self::ONE => 'foo', 'bar', 'quux', ]; } var_dump((new C)->array); ?> array(2) { [0]=> string(3) "bar" [1]=> string(4) "quux" } array(3) { [1]=> string(3) "foo" [2]=> string(3) "bar" [3]=> string(4) "quux" } PHP 5.5 PHP 5.6
  • 8.
    $HTTP_RAW_POST_DATA • $HTTP_RAW_POST_DATAis gone • always_populate_raw_post_data = -1 • Replace it by file_get_contents(php://input) • SOAP, JSON, AMP, any protocol specific Web server $HTTP_RAW_POST_DATA $_POST Script
  • 9.
    charset directives iconv.input_encoding= ISO-8859-1 iconv.internal_encoding = ISO-8859-1 iconv.output_encoding = ISO-8859-1 mbstring.internal_encoding = UTF-8 mbstring.http_input = UTF-8 mbstring.http_output = pass default_charset • in php.ini, check for mbstring, iconv directives are replace by default_charset
  • 10.
    Check in yourcode • defaut_charset • Search for ini_set, ini_get, ini_get_all, ini_restore, get_cfg_var • Seach in php.ini, .htaccess
  • 11.
    htmlentities • PHP5.3 : htmlentities uses iso-8859-1 • PHP 5.4 : htmlentities uses UTF-8 • PHP 5.6 : htmlentities uses default_charset • default_charset uses UTF-8 • htmlentities, htmlhtml_entity_decode and htmlspecialchars should have default values specified
  • 12.
    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(); ?> $ 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
  • 13.
    Easy to spot Strict Standards: Non-static method A::f() should not be called statically in test.php on line 6 Use the E_DEPRECATED while in DEV Keep updated
  • 14.
    Changed behavior •json_decode is stricter • it was more tolerant before with TRUE or False values • gmp resources are object • and not resources (is_resource()) • mcrypt requires valid keys and vectors • check correct size and vector presence
  • 15.
    Upgraded versions •PRCE : 8.34 • oniguruma 5.9.5 • libmagic : 5.17 • Fixed 180+ bugs • http://php.net/ChangeLog-5.php
  • 16.
    Added structures FUNCTIONSCLASSES 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
  • 17.
    New features •Fixing • Modernization • New feature
  • 18.
  • 19.
    Power operator •Replaces pow() • Be aware of precedence $a = pow(2, 3); $a = 2; $a **= 3; $a = 2 ** 3; $z = 1 * 2 ** 3 + 4 ** 5 ;
  • 20.
    … 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
  • 21.
    Variadic … •replaces call_user_func_array • Easier to read • Works on functions • Works with typehint • Doesn’t work with references or default values 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
  • 22.
  • 23.
    __debugInfo() somePasswordSafe Object ( [user] => secret [password] => ********** ) class somePasswordSafe { 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'));
  • 24.
    use const /functions • Importing constants or functions from another namespace • Keep things separated • Avoid polluting global namespace • Avoid static only classes namespace NameSpace { const FOO = 42; function f() { echo __FUNCTION__."n"; } } namespace { use const NameSpaceFOO; use function NameSpacef; echo FOO."n"; f(); }
  • 25.
    Constant scalar expressions <?php class Version { const MAJOR = 2; const MIDDLE = ONE; const MINOR = 1; const FULL = Version::MAJOR.'.'.Version::MIDDLE.'.'.Version::MINOR.'-'._VERSION; static $SHORT = null; function __construct() { self::$SHORT = Version::MAJOR.'.'.Version::MIDDLE; } } ?>
  • 26.
    Constant scalar expressions • Code automation • Won’t accept functioncalls, variables, structures • Keep it simple
  • 27.
    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; • Code automation • Won’t accept functioncalls, variables • Keep it simple public function f($a = (Version::MAJOR == 2) ? 3 : Version::MINOR ** 3) { return $a; } }
  • 28.
    Context changes •PHP 5.6 • Windows XP and 2003 dropped • Support for Zend Optimiser • Uploads over 2 Gb
  • 29.
    Strategies for upgrading? • Lint your code • Probably won’t find much • Review the slides • Make sure your frameworks and libraries are ready too
  • 30.