Damien seguy php 5.6

Post on 28-Jun-2015

437 views 0 download

Tags:

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 of Damien seguy php 5.6

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

• 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

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

$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

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

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

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

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

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

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 = 12 ^ 3 = 83 ^ 3 = 274 ^ 3 = 645 ^ 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 = 12 ^ 3 = 83 ^ 3 = 274 ^ 3 = 645 ^ 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 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(); }

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;    } }

?>

Constant scalar expressions

• Code automation

• Won’t accept functioncalls, variables, structures

• Keep it simple

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;     } }

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/dseguydamien.seguy@gmail.com