About PHP 5.4 • New features • Traits • Built-in webserver • New array syntax and dereferencing • And more! • Removed some nonsense • This talk covers the best bits* * an entirely subjective selection
Array Dereferencing When an array is returned, immediately pick an element from it function getList() { return ['Grumpy', 'Sleepy', 'Bashful', 'Doc']; } // grab first item from list $item = getList()[0]; echo $item; // Grumpy;
PHP Versions Speed Comparison Benchmarks made using: • Newly-compiled vanilla PHP binaries • The bench.php script in the PHP source tree • A rather average laptop • 10 runs per version, then averaged
Traits Re-usable groups of methods, in languages with single inheritance. • Declare a trait, it looks like a class • Add the trait to your class using the use keyword • Methods are available!
Using Traits Apply it like this: class Magic { use \Zend\EventManager\ProvidesEvents; // more magical things } $magic = new Magic(); $magic->setEventManager(new FancyEventManager());
Other Trait Trivia • They can be aliased when used • There are rules about resolving naming clashes • Traits can include properties • Traits can include abstract methods
Other Trait Trivia • They can be aliased when used • There are rules about resolving naming clashes • Traits can include properties • Traits can include abstract methods • Traits can themselves make use of other traits
Built In Webserver Built in application server for PHP 5.4 • Simple • Lightweight • Development use only From the manual: "Requests are served sequentially." http://lrnja.net/NQoXsh
Webserver Examples Use a routing file (example from http://lrnja.net/LigI4U) if (file_exists(__DIR__ . '/' . $_SERVER['REQUEST_URI'])) { return false; // serve the requested resource as-is } else { include_once 'index.php'; } routing.php php -S localhost:8080 routing.php The webserver runs routing.php before entering the requested script. This example serves any existing resource, or routes to index.php
Tracking Upload Progress In a separate file, we can check the relevant session variables to see how the upload is going: http://lrnja.net/Lhs7jJ array(1) { ["upload_progress_123"]=> array(5) { ["start_time"]=> int(1340542011) ["content_length"]=> int(1529158981) ["bytes_processed"]=> int(1386660589) ["done"]=> bool(false) ["files"]=> array(1) { [0]=> array(7) { ... } } } }
Typehinting We can typehint in PHP, on: • Classes • Interfaces • Arrays • ... and now Callable Callable denotes anything that can be called - e.g. a closure, callback or invokable object
Anonymous Functions • Literally functions with no name • More convenient than create_function() Example: $ping = function() { echo "ping!"; }; $ping(); // ping!
Callable Examples function sparkles(Callable $func) { $func(); return "fairy dust"; } $ping = function() { echo "ping!"; }; $pong = "pong"; echo sparkles($ping); // ping!fairy dust echo sparkles($pong); Catchable fatal error: Argument 1 passed to sparkles() must be callable, string given, called in /home/lorna/.../callable.php on line 16 and defined in /home/lorna/.../callable.php on line 10
Sleep and Wakeup When we serialize() or unserialize() an object, PHP provides "magic methods" for us to hook into (this isn’t new): • __sleep() to specify which fields should be serialized • __wakeup() to perform any operations needed to complete an object when it is unserialized This gives us the same feature for when we JSON something
The JsonSerializable Interface From the manual: JsonSerializable { abstract public mixed jsonSerialize () } Objects implementing the JsonSerializable interface can control how they are represented in JSON when they are passed to json_encode()