What's new in PHP 5.5

24
What’s new in PHP 5.5 Tom Corrigan @thetommygnr Melbourne PHP Users Group May 2013 Wednesday, 22 May 13

description

 

Transcript of What's new in PHP 5.5

Page 1: What's new in PHP 5.5

What’s new in PHP 5.5Tom Corrigan

@thetommygnrMelbourne PHP Users Group

May 2013

Wednesday, 22 May 13

Page 2: What's new in PHP 5.5

Overview

• What’s new

• What’s gone

• What’s deprecated

• Current status of PHP 5.5

Wednesday, 22 May 13

Page 3: What's new in PHP 5.5

Generators

• Syntactical sugar to avoid iterator boilerplate

• Introduce a new keyword yield

• https://wiki.php.net/rfc/generators

Wednesday, 22 May 13

Page 4: What's new in PHP 5.5

Before generatorsfunction 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

Page 5: What's new in PHP 5.5

Generator in actionfunction 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

Page 6: What's new in PHP 5.5

Generator free approachfunction doShit($filename){

if (!$fileHandle = fopen($fileName, 'r')) { return; }  while (false !== $line = fgets($fileHandle)) { // do something with $line }  fclose($fileHandle);}

Wednesday, 22 May 13

Page 7: What's new in PHP 5.5

Generators - my opinion

• Few use cases

• Every example I found demonstrated the functionality rather than its utility

• More readable alternatives

Wednesday, 22 May 13

Page 8: What's new in PHP 5.5

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

Page 9: What's new in PHP 5.5

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

Page 10: What's new in PHP 5.5

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

Page 11: What's new in PHP 5.5

Finally block is always executed

try { return 2;} finally { echo "this will be called\n";}//this will never be calledecho "you can not see me";

this will be called//return int(2)

Wednesday, 22 May 13

Page 12: What's new in PHP 5.5

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

Page 13: What's new in PHP 5.5

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

Page 14: What's new in PHP 5.5

APCu

• APC without the bytecode cache (userland caching only)

• PHP API maintains BC with APC

• https://github.com/krakjoe/apcu

Wednesday, 22 May 13

Page 15: What's new in PHP 5.5

DateTimeImmutable

• DateTime

$dt = new \DateTime(‘2013-05-21’);$dt2 = $dt->modify(‘+1 day’);echo $dt->format(‘Y-m-d’); //2013-5-22echo $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-21echo $dt2->format(‘Y-m-d’); //2013-5-22

• Warning: Extends \DateTime, type hints won’t save youWednesday, 22 May 13

Page 16: What's new in PHP 5.5

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

Page 17: What's new in PHP 5.5

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/foreachlistWednesday, 22 May 13

Page 18: What's new in PHP 5.5

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

Page 19: What's new in PHP 5.5

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

Page 20: What's new in PHP 5.5

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

Page 21: What's new in PHP 5.5

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

Page 22: What's new in PHP 5.5

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

Page 23: What's new in PHP 5.5

Current status

• First Release Candidate: 9 May

• Second RC due out on 23rd May

• Expected stable release soon

Wednesday, 22 May 13

Page 24: What's new in PHP 5.5

Questions?

Wednesday, 22 May 13