Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP 5.4

2,585 views

Published on



Presentación sobre los nuevos features de PHP 5.4

- Rendimiento
- Nuevas palabras reservadas
- Formato para nros binarios
- Interfaz JsonSerializable
- session_status
- SessionHandlerInterface
- Hight precision timer
- Clases, Closures & Arrays
- Built-in Server
- Traits
- Opciones y funciones deprecadas

Published in: Technology
  • Be the first to comment

PHP 5.4

  1. 1. PHP 5.4
  2. 2. Federico Lozada Mostomostofreddy@gmail.com@mostofreddyhttp://www.mostofreddy.com.ar
  3. 3. PHP 5.4Rocks!
  4. 4. 40 %más rápido
  5. 5. empty_loop 0.369 empty_loop 0.320 empty_loop 0.196func() 1.318 0.948 func() 0.748 0.428 func() 0.654 0.458undef_func() 1.635 1.265 undef_func() 0.760 0.440 undef_func() 0.599 0.403int_func() 1.068 0.698 int_func() 0.703 0.384 int_func() 0.682 0.486$x = self::$x 0.878 0.509 $x = self::$x 0.685 0.366 $x = self::$x 0.408 0.212self::$x = 0 0.818 0.448 self::$x = 0 0.764 0.445 self::$x = 0 0.487 0.291isset(self::$x) 0.843 0.474 isset(self::$x) 0.639 0.319 isset(self::$x) 0.484 0.288empty(self::$x) 0.879 0.509 empty(self::$x) 0.690 0.370 empty(self::$x) 0.379 0.184$x = Foo::$x 1.107 0.737 $x = Foo::$x 0.987 0.667 $x = Foo::$x 0.371 0.176Foo::$x = 0 1.054 0.685 Foo::$x = 0 1.084 0.765 Foo::$x = 0 0.351 0.155isset(Foo::$x) 1.041 0.672 isset(Foo::$x) 0.928 0.608 isset(Foo::$x) 0.322 0.126empty(Foo::$x) 1.051 0.682 empty(Foo::$x) 0.970 0.651 empty(Foo::$x) 0.346 0.150self::f() 1.412 1.043 self::f() 1.085 0.765 self::f() 0.622 0.426Foo::f() 1.725 1.355 Foo::f() 1.210 0.890 Foo::f() 0.597 0.401$x = $this->x 0.849 0.479 $x = $this->x 0.752 0.433 $x = $this->x 0.394 0.198$this->x = 0 0.976 0.607 $this->x = 0 0.722 0.402 $this->x = 0 0.528 0.332$this->x += 2 0.824 0.455 $this->x += 2 0.632 0.312 $this->x += 2 0.393 0.197++$this->x 0.704 0.335 ++$this->x 0.587 0.267 ++$this->x 0.356 0.161--$this->x 0.722 0.352 --$this->x 0.640 0.320 --$this->x 0.357 0.161$this->x++ 0.737 0.367 $this->x++ 0.633 0.314 $this->x++ 0.381 0.185$this->x-- 0.736 0.366 $this->x-- 0.631 0.311 $this->x-- 0.396 0.200isset($this->x) 0.814 0.445 isset($this->x) 0.684 0.365 isset($this->x) 0.418 0.222empty($this->x) 0.816 0.446 empty($this->x) 0.662 0.342 empty($this->x) 0.426 0.230$this->f() 1.418 1.048 $this->f() 0.937 0.617 $this->f() 0.733 0.537$x = Foo::TEST 0.381 0.011 $x = Foo::TEST 0.555 0.235 $x = Foo::TEST 0.395 0.199new Foo() 2.692 2.323 new Foo() 2.393 2.074 new Foo() 1.360 1.164$x = TEST 0.674 0.305 $x = TEST 0.596 0.276 $x = TEST 0.284 0.089$x = $_GET 0.718 0.349 $x = $_GET 0.546 0.226 $x = $_GET 0.404 0.208$x = $GLOBALS[v] 1.018 0.648 $x = $GLOBALS[v] 0.856 0.536 $x = $GLOBALS[v] 0.576 0.380$x = $hash[v] 0.788 0.418 $x = $hash[v] 0.592 0.272 $x = $hash[v] 0.440 0.244$x = $str[0] 1.291 0.922 $x = $str[0] 0.839 0.520 $x = $str[0] 0.606 0.410------------------------ ------------------------ ------------------------PHP 5.2.9: 31.355 PHP 5.3: 24.830 PHP 5.4: 14.946
  6. 6. FuncionesPalabras recervadas Features
  7. 7. hex2bin()trait http_response_codes()callable get_declared_traits()insteadof getimagesizefromstring() trait_exists() header_register_callback() class_uses() session_status() session_register_shutdown() mysqli_error_list() mysqli_stmt_error_list() etc...
  8. 8. Formato paranúmeros binarios <?php $nroBinario = 0b10; echo $nroBinario.PHP_EOL; //return 2
  9. 9. Interfaz JsonSerializable
  10. 10. <?phpclass Freddy implements JsonSerializable{ public $data = []; public function __construct() { $this->data = array( Federico, Lozada, Mosto ); } public function jsonSerialize() {return $this->data;}}echo json_encode(new Freddy());//return ["Federico","Lozada","Mosto"]//PHP < 5.4//{"data":["Federico","Lozada","Mosto"]}
  11. 11. Sesiones- session_status- session_handler
  12. 12. Session Status<?phpfunction status() { $status = session_status(); if($status == PHP_SESSION_DISABLED) { echo "Session is Disabled"; } else if($status == PHP_SESSION_NONE ) { echo "Session Enabled but No Session values Created"; } else { echo "Session Enabled and Session values Created"; }}status();//return Session Enabled but No Session values Createdsession_start();status();//return Session Enabled and Session values Created
  13. 13. Interfaz de Handler de Sessiones Nativa S N TE <?phpA $obj = new MySessionHandler; session_set_save_handler( array($obj, "open"), array($obj, "close"), array($obj, "read"), array($obj, "write"), array($obj, "destroy"), array($obj, "gc") );
  14. 14. Interfaz de Handler de Sessiones Nativa R A O MySessionHandler H class <?phpA implements SessionHandlerInterface { public function open($savePath, $sessionName) {} public function close() {} public function read($id) {} public function write($id, $data) {} public function destroy($id) {} public function gc($maxlifetime) {} } $handler = new MySessionHandler(); session_set_save_handler($handler, true); session_start();
  15. 15. High Precision Timer
  16. 16. <?php// PHP < 5.4$start = microtime(1);sleep(2);echo "time: ", (microtime(1) - $start);//return time: 2.0010209083557// PHP >= 5.4sleep(2);$start = $_SERVER[REQUEST_TIME_FLOAT];echo "time: ".(microtime(1) - $start);//return time: 2.0010209083557
  17. 17. Clases &Closures & Arrays
  18. 18. Acceso a metodos en la instanciación<?phpclass Test{ public function foo(){ return foo;}}echo (new Test())->foo();//return foo
  19. 19. Callable Type Hint<?phpclass Test { public function foo() {return foo;} static public function bar() {return bar;} public function __invoke(){return invoke;}}function run(callable $func) { echo $func();}$o = new Test;$var = excepcion;run([Test, bar]); //return barrun([$o, foo]); //return foorun($o); //return invokerun($var); //Catchable fatal error: Argument //1 passed to run() must be callable
  20. 20. Closures & $this<?phpclass Test { protected $name = Mostofreddy; public function getName() { $callback = function() {return $this->name;}; return $callback; }}$o = new Test;$func = $o->getName();echo $func(); //return Mostofreddy
  21. 21. Closure::bindTo<?phpclass A { function __construct($val) {$this->val = $val;} function getClosure() { return function() { return $this->val; }; }}$ob1 = new A(1);$ob2 = new A(2);$func = $ob1->getClosure();echo $func(); //return 1$func = $func->bindTo($ob2);echo $func(); //return 2
  22. 22. Class::{expr}() syntax<?phpclass Test { static public function foo(){ return "method foo";} static public function bar(){ return "method bar";}}$method = true;echo Test::{($method)?foo:bar}();//return method foo
  23. 23. Sintaxis de array compactos //return array(6) { [0]=><?php int(0)$array = [0, 1, 2, 3, 4]; [1]=>var_dump($array); int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) }
  24. 24. Array por referencia (Array Deferencing)<?php$txt = "Erase una vez";echo explode(" ", $txt)[0]; //return Eraseecho PHP_EOL;function getName() { return array( usuario => array( nombre=>Federico ) );}echo getName()[usuario][nombre]; //return Federicoecho PHP_EOL;
  25. 25. Built-in web server
  26. 26. ~/www$ php -S localhost:8080PHP 5.4.0 Development Server started at Mon Apr 2 11:37:48 2012Listening on localhost:8080Document root is /var/wwwPress Ctrl-C to quit.TIP: para usarlo desde una virtual hayque poner 0.0.0.0
  27. 27. ~/www$ vim server.sh#! /bin/bashDOCROOT="/var/www"HOST=0.0.0.0PORT=80ROUTER="/var/www/router.php"PHP=$(which php)if [ $? != 0 ] ; then echo "Unable to find PHP" exit 1fi$PHP -S $HOST:$PORT -t $DOCROOT $ROUTER
  28. 28. TRAITS
  29. 29. Ejemplo simple<?phptrait Log { public function addLog($m) {echo LOG: .$m;}}class Test { use Log; public function foo() { $this->addLog(foo); }}$obj = new Test;$obj->foo();//return LOG: foo
  30. 30. Multiple Traits<?phptrait Log { public function addLog($m) {echo LOG: .$m;}}trait Mensaje { public function holaMundo() {return "Hola Mundo!";}}class Test { use Log, Mensaje; public function foo() { $this->addLog($this->holaMundo()); }}$obj = new Test;$obj->foo();//return LOG: Hola Mundo!
  31. 31. Traits: Composicion<?phptrait File { public function put($m) {error_log($m, 3, /tmp/log);}}trait Log { use File; public function addLog($m) {$this->put(LOG: .$m);}}class Test { use Log; public function foo() { $this->addLog(test);}}$obj = new Test;$obj->foo();//return LOG: test
  32. 32. Traits: Herencia<?phptrait Hello { public function foo () {return "traits";} public function foo_1() { return $this->foo()." - ".parent::foo(); }}class Base { public function foo() {return base;}}class Test extends Base { use Hello; public function foo() {return Test;}}$o = new Test;echo $o->foo(); //return Testecho $o->foo_1(); //return Test - base
  33. 33. Traits: Resolviendo conflictos<?phptrait Game { public function play() {return "Play Game";}}trait Music { public function play() {return "Play Music";}}class Player { use Game, Music;}$o = new Player;echo $o->play();
  34. 34. Traits: Resolviendo conflictos <?php trait Game { public function play() {return "Play Game";} } trait Music { public function play() {return "Play Music";} } class Player { use Game, Music; } $o = new Player; PHP no resuelve el echo $o->play(); conflicto automáticamentePHP Fatal error: Trait method play has not been applied, because there are collisions with other trait methods on Player in /var/www/test/test_traits.php on line 10
  35. 35. Traits: Resolviendo conflictos - insteadoftrait Game { public function play() {return "Play Game";}}trait Music { public function play() {return "Play Music";}}class Player { use Game, Music { Music::play insteadof Game; }}$o = new Player;echo $o->play();//return Play Music
  36. 36. Traits: Resolviendo conflictos - rename<?phptrait Game { public function play() {return "Play Game";}}trait Music { public function play() {return "Play Music";}}class Player { use Game, Music { Game::play as gamePlay; Music::play insteadof Game; }}$o = new Player;echo $o->play(); //return Play Musicecho $o->gamePlay(); //return Play Game
  37. 37. Traits: Atributos<?phptrait Usuario { protected $nombre; public function getName(){ return $this->nombre;}}class Empleado { use Usuario; public function setName($nombre) { $this->nombre = $nombre; }}$o = new Empleado;$o->setName(Federico);echo $o->getName();//return Federico
  38. 38. ini optionsSafe mode and all related optionsmagic_quotes_gpcmagic_quotes_runtimemagic_quotes_sybaseregister_globalsregister_long_arraysdefine_syslog_variablesy2k_compliance
  39. 39. Functions & Extensionessession_is_registered()session_register()session_unregister()define_syslog_variables()get_magic_quotes_gpc && get_magic_quotes_runtimesiempre devuelven falseimport_request_variables()mysqli_bind_param()mysqli_bind_result()mysqli_fetch()Sqlite (no afecta a sqlite3)
  40. 40. Para los temerosos...Tutorial para instalar demanera fácil variasversiones de PHPen un mismo servidor:http://bit.ly/HWJ5lW
  41. 41. ¿Preguntas?

×