What's New in PHP 5.5



What's New in PHP 5.5

0 2


whats-new-in-php-5.5-presentation

A presentation on What's New in PHP 5.5

On Github cballou / whats-new-in-php-5.5-presentation

What's New in PHP 5.5

Corey Ballou  ·  @cballou

· Queen City PHP co-organizer · POP.co employee · Stop me to ask questions

Generators

Support for generators has been added via the yield keyword

range(0, 1000000) vs. user supplied function

· Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface middot; Allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory · Useful in avoiding memory limitations, i.e.

Example Usage of Yield Keyword

function getLines($filepath) {
    $f = fopen($filepath, 'r');
    try {
        while ($line = fgets($f)) {
            yield $line;
        }
    } finally {
        fclose($f);
    }
}

foreach (getLines("file.txt") as $n => $line) {
    echo $line;
}

The "Finally" Keyword is Finally Here

Execute code ALWAYS.

try {
    throw new Exception('hello');
} catch (Exception $e) {
    echo $e->getMessage();
} finally {
    // this code will always be run
    echo ', world';
}
· Code that should be run regardless of whether an exception has been thrown or not. · Useful for things like closing file descriptors or resources. · Also useful for cleanup code in a method/function. · You can nest try/catch/finally and throw a new exception · Check the last slide!

Password Hashing API

A super easy library that uses underlying crypt library.

  • password_get_info()
  • password_hash()
  • password_needs_rehash()
  • password_verify()
· Best practice cryptography to the masses. · No dealing with messy ciphertext

Example of Register/Login

function register($username, $password) {
    $hash = password_hash($password, PASSWORD_BCRYPT);
    // save hash to database
}

function login($username, $password) {
    $hash = getHashFromDbByUsername($username);
    if (password_verify($password, $hash)) {
        // perform login (store session var)
        return true;
    }
    return false;
}

Increasing Password Security

You can optionally supply your own salt and algorithmic cost.

function register($username, $password) {
    $options = array('salt' => 'someRandomSalt', 'cost' => 12);
    $hash = password_hash($password, PASSWORD_BCRYPT, $options);
    // save hash to database
}
· Default cost is 10. · Cost refers to "work factor". · Work factor affects resulting hash. · Work factor increases the computation time.

Example of Upgrading Your Hashing Algorithm

function login($username, $password) {
    $hash = getHashFromDbByUsername($username);
    if (password_verify($password, $hash)) {
        // check if hash is in updated format
        if (password_needs_rehash($hash, PASSWORD_BCRYPT)) {
            // perform update
            $hash = password_hash($password, PASSWORD_BCRYPT);
            // save new hash to database
        }

        // perform login (store session var)
        return true;
    }
    return false;
}

Foreach + List

$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as list($a, $b)) {
    echo "A: $a; B: $b\n";
}

array_column() fun

$people = [
    [
        'firstname' => 'John',
        'lastname' => 'Doe'
    ],
    [
        'firstname' => 'Jane',
        'lastname' => 'Doe'
    ],
];

// contains [ 0 => 'John', 1 => 'Jane' ]
$firstnames = array_column($people, 'firstname');

Improvements to empty()

function always_false() {
    return false;
}

if (empty(always_false())) {
    echo "Hello, world.";
}

Array and String Literal Dereferencing

// array dereferencing
echo [1, 2, 3][0];

// string dereferencing
echo 'PHP'[0];

Now let's really get crazy...

function foo() {
    return array(1, 2, 3);
}
echo foo()[2]; // prints 3

$func = function() { return array('a', 'b', 'c'); };
echo $func()[0]; // prints a

Zend Optimiser+ OPCache Extension

$ php -v
PHP 5.4.17RC1 (cli) (built: Jun 22 2013 19:27:26)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technologies

So, uh, what is an opcode cache?

  • Component designed to speed up performance of PHP without altering the app.
  • Overrides PHP's default compiler callback by checking if a compiled intermediate-code version of the code is available in-memory.
  • It skips compilation when it can!
· Uses shared memory for storage, and can execute files directly off of it - without having to 'unserialize' the code.

What to do about deprecated APC module?

APC User Cache is in the works:

github.com/krakjoe/apcu

APC minus the opcode cache!

· Zend Optimizer+ has a consistent 5-20% rps performance edge over APC

Backwards Incompatible Changes

· Windows builds now require Windows Vista or newer · If previously using "a" format with unpack, need to use version_compare() to swap "a" for "Z"

CREDITS

QUESTIONS? COMMENTS?

Thanks for pretending to enjoy my banter!