damien seguy php 5.6

30
PHP 5.6 what’s new? Zoetemer, Nederlands, November 7th 2014 Damien Seguy

Upload: damien-seguy-

Post on 28-Jun-2015

439 views

Category:

Technology


0 download

DESCRIPTION

All new features from PHP 5.6, if you already have upgraded to PHP 5.5. Power operator, array keys, constant scalar expressions,

TRANSCRIPT

Page 1: Damien seguy php 5.6

PHP 5.6 what’s new?Zoetemer, Nederlands, November 7th 2014

Damien Seguy

Page 2: Damien seguy php 5.6

What’s new in PHP 5.6?

• Changing version is often a big challenge

• Backward incompatibilities

• New features

• How to spot them ?

Page 3: Damien seguy php 5.6

Speaker

• Damien Seguy

• CTO at exakat

• Phather of the plush toy elePHPant

• PHP static auditing

Page 4: Damien seguy php 5.6

What will change?

• Removed features

• Deprecated features

• Changed features

• New features

Page 5: Damien seguy php 5.6

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

Page 6: Damien seguy php 5.6

Deprecated

• Not removed yet!

• ext/mysql was deprecated in 5.5

• It is still in the code!

• Will generate errors when using it

• Set your error_reporting to E_STRICT when coding!

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

Page 7: Damien seguy php 5.6

Array keys won’t be overwritten

<?phpclass 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

Page 8: Damien seguy 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

$_POSTScript

Page 9: Damien seguy php 5.6

charset directives

• in php.ini, check for mbstring, iconv directives are replace by default_charset

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

Page 10: Damien seguy php 5.6

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

Page 11: Damien seguy php 5.6

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

Page 12: Damien seguy php 5.6

Incompatible context

<?php  class A {       function f() { echo get_class($this); }  }  A::f();  ?>

$ php53 test.php

Notice: Undefined variable: this in test.php on line 3 A

$ 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

Page 13: Damien seguy php 5.6

Easy to spot

Use the E_DEPRECATED while in DEV Keep updated

Strict Standards: Non-static method A::f() should not be called statically in test.php on line 6

Page 14: Damien seguy php 5.6

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

Page 15: Damien seguy php 5.6

Upgraded versions

• PRCE : 8.34

• oniguruma 5.9.5

• libmagic : 5.17

• Fixed 180+ bugs

• http://php.net/ChangeLog-5.php

Page 16: Damien seguy php 5.6

Added structuresFUNCTIONS 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

Page 17: Damien seguy php 5.6

New features

• Fixing

• Modernization

• New feature

Page 18: Damien seguy php 5.6

Modernization

Page 19: Damien seguy php 5.6

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 ; 

Page 20: Damien seguy php 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 = 12 ^ 3 = 83 ^ 3 = 274 ^ 3 = 645 ^ 3 = 125

Page 21: Damien seguy php 5.6

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 = 12 ^ 3 = 83 ^ 3 = 274 ^ 3 = 645 ^ 3 = 125

Page 22: Damien seguy php 5.6

Really new

Page 23: Damien seguy php 5.6

__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'));

Page 24: Damien seguy php 5.6

use const / functions• Importing constants or

functions from another namespace

• Keep things separated

• Avoid polluting global namespace

• Avoid static only classes

namespace Name\Space {     const FOO = 42;     function f() { echo __FUNCTION__."\n"; } }

namespace {     use const Name\Space\FOO;     use function Name\Space\f;

    echo FOO."\n";     f(); }

Page 25: Damien seguy php 5.6

Constant scalar expressions<?php

class Version {      const MAJOR = 2;      const MIDDLE = ONE;      const MINOR = 1; 

    const FULL = Version::MAJOR.'.'.Version::MIDDLE.'.'.Version::MINOR.'-'.PHP_VERSION; 

    static $SHORT = null; 

   function __construct() {      self::$SHORT = Version::MAJOR.'.'.Version::MIDDLE;    } }

?>

Page 26: Damien seguy php 5.6

Constant scalar expressions

• Code automation

• Won’t accept functioncalls, variables, structures

• Keep it simple

Page 27: Damien seguy php 5.6

Constant scalar expressions

• Code automation

• Won’t accept functioncalls, variables

• Keep it simple

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 = (Version::MAJOR == 2) ? 3 : Version::MINOR ** 3) {         return $a;     } }

Page 28: Damien seguy php 5.6

Context changes

• PHP 5.6

• Windows XP and 2003 dropped

• Support for Zend Optimiser

• Uploads over 2 Gb

Page 29: Damien seguy php 5.6

Strategies for upgrading?

• Lint your code

• Probably won’t find much

• Review the slides

• Make sure your frameworks and libraries are ready too