21
Nouveautés PHP 5.4

Nouveautés PHP 5.4

  • Upload
    ryo

  • View
    54

  • Download
    0

Embed Size (px)

DESCRIPTION

Nouveautés PHP 5.4. Nouveautés PHP 5.2 > 5.3. __callStatic / __invoke Garbage collector ( references circulaires) Late Static Binding Fonctions lamba (!= create_function ) Namespaces. Nouveautés PHP 5.2 > 5.3 – En plus. Optimisations de Perf Phar - PowerPoint PPT Presentation

Citation preview

Page 1: Nouveautés PHP 5.4

Nouveautés PHP 5.4

Page 2: Nouveautés PHP 5.4

Nouveautés PHP 5.2 > 5.3

- __callStatic / __invoke- Garbage collector (references circulaires)- Late Static Binding- Fonctions lamba (!= create_function)- Namespaces

Page 3: Nouveautés PHP 5.4

Nouveautés PHP 5.2 > 5.3 – En plus

- Optimisations de Perf- Phar - ajouts de paramètres optionnels (inversion de

paramètres, etc…)- ajouts de fonctions- ajouts de constantes + SPL, __DIR__, __NAMESPACE__- Bugfixes, securité…

Page 4: Nouveautés PHP 5.4

Nouveautés PHP 5.3 > 5.4

- Indirect method call (!= call_user_func)- Dereferenced array- Class expressions- Traits

----- Closure $this (ajouté en 5.3 puis supprimé

puis rajouté en 5.4)

Page 5: Nouveautés PHP 5.4

PHP 5.3

Page 6: Nouveautés PHP 5.4

__callStatic

Comme la fonction magique __call mais en static !

class Dagobert{public static function aboyer() {

echo "Ouah !"; }

}

$class = ‘Dagobert'; $action = 'aboyer'; $class::$action(); //affiche "Ouah !"

Page 7: Nouveautés PHP 5.4

__invoke

Utilisation d’un objet comme si c’était une fonction

class Dagobert{public function __invoke() {

echo "Ouah !"; }

}

$dagobert = new Dagobert(); //créé une instance de Dagobert$dagobert(); //affiche « Ouah ! »

Page 8: Nouveautés PHP 5.4

Garbage collector (references circulaires)

class LeParent {public function __construct() {

$this->child = new Enfant($this);}

}

class Enfant { public function __construct(LeParent $parent) {

$this->parent = $parent; }

}

Page 9: Nouveautés PHP 5.4

Late Static BindingProblématique

class Foo {static protected $_name = ‘Foo’;public static function test() {

return self::$_name; }

}

class Bar extends Foo { static protected $_name = ‘Bar’;

}

echo Bar::test();

Page 10: Nouveautés PHP 5.4

Late Static BindingSolution

class Foo {static protected $_name = ‘Foo’;public static function test() {

return static::$_name; }

}

class Bar extends Foo { static protected $_name = ‘Bar’;

}

echo Bar::test();

Page 11: Nouveautés PHP 5.4

Fonctions lamba$x = 1; $closure = function() use (&$x) {

++$x; }; echo $x . "\n"; $closure(); echo $x . "\n"; $closure(); echo $x . "\n";

Appelées aussi closure!= create_function()

Page 12: Nouveautés PHP 5.4

Namespaces<?phpnamespace Foo;function bar() {

echo "appel de bar..."; }

\Foo\bar(); // affiche "appel de bar..."

use Foo as ns; ns\bar(); // affiche "appel de bar..."

use Foo; bar(); // affiche "appel de bar..."

Page 13: Nouveautés PHP 5.4

Namespaces

<?php namespace Foo; function strrev($string) { return $string; }

$string = ‘string’;echo strrev($string); //affiche …… ?

Page 14: Nouveautés PHP 5.4

Namespaces

<?php namespace Foo; function strrev($string) { return $string; }

$string = ‘string’;echo strrev($string); //affiche ‘string’echo \strrev($string); //affiche ‘gnirts’

Ca aurai été plus drôle avec $string = ‘bob’ mais plus difficile à expliquer…

Page 15: Nouveautés PHP 5.4

PHP 5.4

Page 16: Nouveautés PHP 5.4

Indirect method call

class Foo {public function bar($name) {

return "Hi, $name";}

}

$f = array('Foo','bar');echo $f(‘Dagobert'); // Hi, Dagobert

création d’instance !?!= call_user_func()

Page 17: Nouveautés PHP 5.4

Indirect method callclass Gardechampetre {

function __construct($obj) {$this->obj = $obj;

}

public function take() {return " J’ai prît une " . $this->obj;

}}

// old style$regisrobert = new Gardechampetre("pelle");echo $regisrobert->take (); // new cool styleecho (new Gardechampetre("claque"))->take(); //proche de l’interface fluide

Page 18: Nouveautés PHP 5.4

Dereferenced array

class Foo {public function bar($name) {

return [1,2,3];}

}

echo (new Foo)->bar()[1] // affiche 2;

Page 19: Nouveautés PHP 5.4

Class expressionsclass Foo {

public function barAction($name) {return "Hi, $name";

}

public function mooAction($name) {return "Hello, $name";

}}

$obj = new Foo;foreach ([‘bar’, ‘moo’] as $action) {

echo $obj->{$action . ‘Action’}(‘Dagobert’);//echo [$obj, $action . ‘Action’](‘Dagobert’);

}

Page 20: Nouveautés PHP 5.4

Traitstrait Singleton {

protected static $_instance;public static function getInstance() {

if (!self::$_instance instanceof self) {self::$_instance = new self;

}return self::$_instance;

} }class A {

use Singleton; } class B extends ArrayObject {

use Singleton; } A::getInstance(); B::getInstance();

Page 21: Nouveautés PHP 5.4

Traitstrait A{

public function hello() { return ‘hello’; }abstract public function quit();

}trait B {

public function hello() { return ‘Hi’; }}class Test {

use A, B {B::hello insteadof A;A::hello as helloA;helloA as final; //on peut redéfinir tous les accès sauf static

} public function quit() {}

} echo (new Test)->hello() // affiche ‘Hi’;echo (new Test)->helloA() // affiche ‘Hello’;