1 / 21

Nouveautés PHP 5.4

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

ryo
Download Presentation

Nouveautés PHP 5.4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nouveautés PHP 5.4

  2. Nouveautés PHP 5.2 > 5.3 • __callStatic / __invoke • Garbagecollector (references circulaires) • LateStaticBinding • Fonctions lamba(!= create_function) • Namespaces

  3. 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é…

  4. Nouveautés PHP 5.3 > 5.4 • Indirect method call (!= call_user_func) • Dereferencedarray • Class expressions • Traits ---- • Closure $this (ajouté en 5.3 puis supprimé puis rajouté en 5.4)

  5. PHP 5.3

  6. __callStatic Comme la fonction magique __call mais en static ! class Dagobert{ public staticfunction aboyer() { echo "Ouah !"; } } $class = ‘Dagobert'; $action = 'aboyer'; $class::$action(); //affiche "Ouah !"

  7. __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 ! »

  8. Garbagecollector (references circulaires) class LeParent { public function__construct() { $this->child = new Enfant($this); } } class Enfant { public function__construct(LeParent $parent) { $this->parent = $parent; } }

  9. LateStaticBinding Problématique class Foo { staticprotected $_name = ‘Foo’; public staticfunction test() { return self::$_name; } } class Bar extendsFoo { staticprotected $_name = ‘Bar’; } echoBar::test();

  10. LateStaticBinding Solution class Foo { staticprotected $_name = ‘Foo’; public staticfunction test() { return static::$_name; } } class Bar extendsFoo { staticprotected $_name = ‘Bar’; } echoBar::test();

  11. 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()

  12. Namespaces <?php namespaceFoo; 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..."

  13. Namespaces <?php namespace Foo; function strrev($string) { return $string; } $string = ‘string’; echo strrev($string); //affiche …… ?

  14. 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…

  15. PHP 5.4

  16. 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()

  17. Indirect method call class Gardechampetre { function__construct($obj) { $this->obj = $obj; } public functiontake() { return  " J’ai prît une "  . $this->obj; } } // old style $regisrobert = new Gardechampetre("pelle"); echo $regisrobert->take (); // new cool style echo (new Gardechampetre("claque"))->take(); //proche de l’interface fluide

  18. Dereferencedarray • class Foo { • public function bar($name) { • return [1,2,3]; • } • } • echo (new Foo)->bar()[1] // affiche 2;

  19. Class expressions • class Foo { • public functionbarAction($name) { • return "Hi, $name"; • } • public functionmooAction($name) { • return "Hello, $name"; • } • } • $obj = new Foo; • foreach ([‘bar’, ‘moo’] as $action) { • echo $obj->{$action . ‘Action’}(‘Dagobert’); • //echo [$obj, $action . ‘Action’](‘Dagobert’); • }

  20. Traits trait Singleton { protectedstatic $_instance; public staticfunctiongetInstance() { if (!self::$_instanceinstanceof self) { self::$_instance = new self; } return self::$_instance; } } class A { use Singleton; } class B extendsArrayObject { use Singleton; } A::getInstance(); B::getInstance();

  21. Traits trait A{ public function hello() { return ‘hello’; } abstract public functionquit(); } 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 functionquit() {} } echo (new Test)->hello() // affiche ‘Hi’; echo (new Test)->helloA() // affiche ‘Hello’;

More Related