Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP 5.3 to 5.6

PHP 5.3 to 5.6

Federico Lozada Mosto

June 15, 2015
Tweet

More Decks by Federico Lozada Mosto

Other Decks in Programming

Transcript

  1. 5.6.0 – 28/08/2014 5.5.0 – 20/06/2013 5.4.0 – 01/03/2012 5.3.0

    – 30/07/2009 5.2.0 – 02/11/2006 5.1.0 – 24/11/2005 5.0.0 – 13/07/2004 Fuentes: ✓ http://php.net/eol.php ✓ http://php.net/supported-versions.php !
  2. <?php namespace mostofreddy\logger; Class Logger { … } ?> <?php

    $log = new \mostofreddy\logger\Logger(); $log->info(“example of namespaces”); //return example of namespaces PHP 5.3
  3. PHP 5.3 $sayHello = function() { return "Hello world"; };

    echo $sayHello(); //return Hello world $text = "Hello %s"; $sayHello = function($name) use (&$text) { return sprintf($text, $name); }; echo $sayHello("phpbsas"); //return Hello phpbsas
  4. PHP 5.4 class 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"]}
  5. PHP < 5.4 $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") )
  6. PHP 5.4 class MySessionHandler 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();
  7. PHP 5.4 function 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 Created session_start(); status(); //return Session Enabled and Session values Created
  8. PHP 5.4 $array = [0, 1, 2, 3, 4]; var_dump($array);

    //return array(6) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) } Short syntax
  9. PHP 5.4 Array Deferencing $txt = "Hello World"; echo explode("

    ", $txt)[0]; //return Hello World function getName() { return [ 'user' => array( 'name'=>'Federico' ) ]; } echo getName()['user']['name']; //return Federico
  10. PHP 5.4 ~/www$ php -S localhost:8080 PHP 5.4.0 Development Server

    started at Mon Apr 2 11:37:48 2012 Listening on localhost:8080 Document root is /var/www Press Ctrl-C to quit.
  11. PHP 5.4 ~/www$ vim server.sh #! /bin/bash DOCROOT="/var/www" HOST=0.0.0.0 PORT=80

    ROUTER="/var/www/router.php" PHP=$(which php) if [ $? != 0 ] ; then echo "Unable to find PHP" exit 1 fi $PHP -S $HOST:$PORT -t $DOCROOT $ROUTER
  12. PHP 5.4 trait 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
  13. PHP 5.4 trait 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(); Solving confict PHP does not solve conflicts automatically PHP 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
  14. PHP 5.4 trait 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 Music echo $o->gamePlay(); //return Play Game
  15. PHP < 5.5 function getLines($filename) { if (!$handler = fopen($filename,

    'r')) { return; } $lines = []; while (false != $line = fgets($handler)) { $lines[] = $line; } fclose($handler); return $lines; } $lines = getLines('file.txt'); foreach ($lines as $line) { echo $line; }
  16. PHP 5.5 function getLines($filename) { if (!$handler = fopen($filename, 'r'))

    { return; } while (false != $line = fgets($handler)) { yield $line; } fclose($handler); } foreach (getLines('file.txt') as $line) { echo $line; }
  17. PHP with generators Total time: 1.3618 seg Memory: 256 k

    PHP classic Total time: 1.5684 seg Memory: 2.5 M PHP 5.5
  18. PHP 5.5 Advanced configuration for PHPUnit / Symfony / Doctrine

    / Zend / etc opcache.save_comments=1 opcache.enable_file_override=1 Functions opcache_reset() opcache_invalidate($filename, true)
  19. PHP 5.5 $hash = password_hash($password, PASSWORD_DEFAULT); //or $hash = password_hash(

    $password, PASSWORD_DEFAULT, //default bcrypt ['cost'=>12, 'salt'=> 'asdadlashdoahdlkuagssa'] ); $user->setPass($hash); $user->save();
  20. PHP 5.5 $hash = $user->getPass(); if (!password_verify($password, $hash) { throw

    new \Exception('Invalid password'); } if (password_needs_rehash($hash, PASSWORD_DEFAULT, ['cost' => 18])) { $hash = password_hash( $password, PASSWORD_DEFAULT, ['cost' => 18] ); $user->setPass($hash); $user->save(); }
  21. PHP 5.5 var_dump(password_get_info($hash1)); // prints array(3) { 'algo' => int(1)

    'algoName' => string(6) "bcrypt" 'options' => array(1) { 'cost' => int(12) } }
  22. PHP < 5.6 function ides() { $cant = func_num_args(); echo

    "Ides count: ".$cant; } ides('Eclipse', 'Netbeas'); // Ides count 2
  23. PHP 5.6 function ides($ide, ...$ides) { echo "Ides count: ".count($ides);

    } ides ( 'Eclipse', 'Netbeas', 'Sublime' ); // Ides count 2
  24. PHP 5.6 function sum(...$numbers) { $acc = 0; foreach ($numbers

    as $n) { $acc += $n; } return $acc; } echo sum(1, 2, 3, 4); // 10
  25. PHP 5.6 function add($a, $b) { return $a + $b;

    } echo add(...[1, 2]); // 3 $a = [1, 2]; echo add(...$a); // 3
  26. PHP 5.6 function showNames($welcome, Person ...$people) { echo $welcome.PHP_EOL; foreach

    ($people as $person) { echo $person->name.PHP_EOL; } } $a = new Person('Federico'); $b = new Person('Damian'); showNames('Welcome: ', $a, $b); //Welcome: //Federico //Damian
  27. PHP 5.6 class C { private $prop; public function __construct($val)

    { $this->prop = $val; } public function __debugInfo() { return [ 'propSquared' => $this->prop ** 2 ]; } } var_dump(new C(42)); // object(C)#1 (1) { ["propSquared"]=> int(1764) }
  28. FEDERICO LOZADA MOSTO TW: @mostofreddy Web: mostofreddy.com.ar FB: mostofreddy In:

    ar.linkedin.com/in/federicolozadamosto Git: mostofreddy Thanks!