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

What's new in PHP 5.5

Tom
May 22, 2013

What's new in PHP 5.5

These are the slides from the talk delivered to the May 2013 Melbourne PHP User Group meeting

Tom

May 22, 2013
Tweet

More Decks by Tom

Other Decks in Programming

Transcript

  1. What’s new in PHP 5.5 Tom Corrigan @thetommygnr Melbourne PHP

    Users Group May 2013 Wednesday, 22 May 13
  2. Overview • What’s new • What’s gone • What’s deprecated

    • Current status of PHP 5.5 Wednesday, 22 May 13
  3. Generators • Syntactical sugar to avoid iterator boilerplate • Introduce

    a new keyword yield • https://wiki.php.net/rfc/generators Wednesday, 22 May 13
  4. Before generators function getLinesFromFile($fileName) { if (!$fileHandle = fopen($fileName, 'r'))

    { return; } $lines = []; while (false !== $line = fgets($fileHandle)) { $lines[] = $line; } fclose($fileHandle); return $lines; } $lines = getLinesFromFile($fileName); foreach ($lines as $line) { // do something with $line } Wednesday, 22 May 13
  5. Generator in action function getLinesFromFile($fileName) { if (!$fileHandle = fopen($fileName,

    'r')) { return; } while (false !== $line = fgets($fileHandle)) { yield $line; } fclose($fileHandle); } $lines = getLinesFromFile($fileName); foreach ($lines as $line) { // do something with $line } Wednesday, 22 May 13
  6. Generator free approach function doShit($filename){ if (!$fileHandle = fopen($fileName, 'r'))

    { return; } while (false !== $line = fgets($fileHandle)) { // do something with $line } fclose($fileHandle); } Wednesday, 22 May 13
  7. Generators - my opinion • Few use cases • Every

    example I found demonstrated the functionality rather than its utility • More readable alternatives Wednesday, 22 May 13
  8. Finally • New addition to try...catch blocks • Code within

    a finally block will always be executed. Always! • Useful for closing DB connections, file handles etc • https://wiki.php.net/rfc/finally Wednesday, 22 May 13
  9. The problem: $db = mysqli_connect(); try { call_some_function($db); //function may

    throw exceptions which we can ‘t handle } catch (Exception $e) { mysqli_close($db); throw $e; } mysql_close($db); Wednesday, 22 May 13
  10. Finally: a solution $db = mysqli_connect(); try { call_some_function($db);//function may

    throw exceptions which we can ‘t handle } finally { mysqli_close($db); } Wednesday, 22 May 13
  11. Finally block is always executed try { return 2; }

    finally { echo "this will be called\n"; } //this will never be called echo "you can not see me"; this will be called //return int(2) Wednesday, 22 May 13
  12. Opcache • Zend Optimizer+ has been open sourced and renamed

    Opcache • Included by default in PHP 5.5+ • This is a big win, it’s the fastest opcode cache for PHP, between 5%-20% faster than APC • https://wiki.php.net/rfc/optimizerplus Wednesday, 22 May 13
  13. What about APC? • No current plans to support PHP

    > 5.4 • Does it matter? • Was always behind new PHP releases by months • Opcache is faster • Opcaches ships with PHP 5.5+ Wednesday, 22 May 13
  14. APCu • APC without the bytecode cache (userland caching only)

    • PHP API maintains BC with APC • https://github.com/krakjoe/apcu Wednesday, 22 May 13
  15. DateTimeImmutable • DateTime $dt = new \DateTime(‘2013-05-21’); $dt2 = $dt->modify(‘+1

    day’); echo $dt->format(‘Y-m-d’); //2013-5-22 echo $dt2->format(‘Y-m-d’); //2013-5-22 • DateTimeImmutable $dt = new \DateTimeImmutable(‘2013-05-21’); $dt2 = $dt->modify(‘+1 day’); echo $dt->format(‘Y-m-d’); //2013-5-21 echo $dt2->format(‘Y-m-d’); //2013-5-22 • Warning: Extends \DateTime, type hints won’t save you Wednesday, 22 May 13
  16. Constant array and string dereferencing You can now do this:

    echo [“one”, “two”, “three”][1]; => “two” And this: echo “onetwothree”[1]; => “n” Wednesday, 22 May 13
  17. list() inside foreach $talks = [ [ "speaker" => "Tom

    Corrigan", "talk" => "What’s new in PHP 5.5", ], [ "speaker" => "Rick Measham", "talk" => "Hacker tips for getting hired", ], ]; foreach ($talks as list($speaker, $talk){ echo $speaker. " on " . $talk . PHP_EOL; } // Tom Corrigan on What’s new in PHP 5.5 // Rick Measham on Hacker tips for getting hired •https://wiki.php.net/rfc/foreachlist Wednesday, 22 May 13
  18. array_column() $talks = [ [ "speaker" => "Tom Corrigan", "talk"

    => "What’s new in PHP 5.5", ], [ "speaker" => "Rick Measham", "talk" => "Hacker tips for getting hired", ], ]; $speakers = array_column($talks, "speaker"); //array [0] Tom Corrigan [1] Rick Measham • https://wiki.php.net/rfc/array_column Wednesday, 22 May 13
  19. Password hash • Colby did a talk last month •

    Use it today in PHP 5.3.7+ https://github.com/ircmaxell/ password_compat Wednesday, 22 May 13
  20. Tons of new stuff in ext/intl • Didn’t get time

    to cover this one, sorry! • See: http://www.php.net/manual/en/migration55.classes.php Wednesday, 22 May 13
  21. Removed/changed • No Windows XP or 2003 support • Case

    insensitivity no longer locale specific • Logo functions removed: php_logo_guid(), php_egg_logo_guid() etc • Changes to pack() and unpack() BC BREAK • http://www.php.net/manual/en/migration55.incompatible.php Wednesday, 22 May 13
  22. Deprecated • ext/mysql !!!! • preg_replace() /e modifier, use preg_replace_callback()

    (good!) • IntlDateFormatter::setTimeZoneID() and datefmt_set_timezone_id() • some mcrypt functions • http://www.php.net/manual/en/migration55.deprecated.php Wednesday, 22 May 13
  23. Current status • First Release Candidate: 9 May • Second

    RC due out on 23rd May • Expected stable release soon Wednesday, 22 May 13