nouveautés php 5.4
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 PresentationTRANSCRIPT
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 - ajouts de paramètres optionnels (inversion de
paramètres, etc…)- ajouts de fonctions- ajouts de constantes + SPL, __DIR__, __NAMESPACE__- Bugfixes, securité…
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)
PHP 5.3
__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 !"
__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 ! »
Garbage collector (references circulaires)
class LeParent {public function __construct() {
$this->child = new Enfant($this);}
}
class Enfant { public function __construct(LeParent $parent) {
$this->parent = $parent; }
}
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();
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();
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()
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..."
Namespaces
<?php namespace Foo; function strrev($string) { return $string; }
$string = ‘string’;echo strrev($string); //affiche …… ?
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…
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()
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
Dereferenced array
class Foo {public function bar($name) {
return [1,2,3];}
}
echo (new Foo)->bar()[1] // affiche 2;
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’);
}
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();
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’;