Slide 1

Slide 1 text

The PHP 5.4 Features You Will Actually Use

Slide 2

Slide 2 text

About Me • Lorna Jane Mitchell • PHP Consultant/Developer • Author of PHP Master • Twitter: @lornajane • Website: http://lornajane.net

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Start With The Easy Bit

Slide 5

Slide 5 text

New Array Syntax

Slide 6

Slide 6 text

Array Syntax We had this: $game[] = 'paper'; $game[] = 'scissors'; $game[] = 'stone'; print_r($game); $game[0] = 'paper'; $game[1] = 'scissors'; $game[2] = 'stone'; print_r($game);

Slide 7

Slide 7 text

Array Syntax Or this: $game = array('stone', 'paper', 'scissors'); print_r($game); $game = array(0 => 'stone', 1 => 'paper', 2 => 'scissors'); print_r($game);

Slide 8

Slide 8 text

Array Syntax Now we can do: $game = ['scissors', 'stone', 'paper']; print_r($game); $game = [0 => 'scissors', 1 => 'stone', 2 => 'paper']; print_r($game);

Slide 9

Slide 9 text

Array Dereferencing

Slide 10

Slide 10 text

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;

Slide 11

Slide 11 text

PHP 5.4 Stealth Feature

Slide 12

Slide 12 text

PHP 5.4 Is Faster

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

PHP Versions Speed Comparison

Slide 15

Slide 15 text

PHP Versions Speed Comparison

Slide 16

Slide 16 text

PHP Versions Speed Comparison

Slide 17

Slide 17 text

PHP Versions Speed Comparison

Slide 18

Slide 18 text

Traits

Slide 19

Slide 19 text

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!

Slide 20

Slide 20 text

Simplest Trait Example getAuditTrail();

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Built In Webserver

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Webserver Examples Start a simple server on a port number of your choice php -S localhost:8080 http://localhost:8080

Slide 25

Slide 25 text

Webserver Examples Change the hostname: php -S dev.project.local:8080 http://dev.project.local:8080

Slide 26

Slide 26 text

Webserver Examples Specify the docroot php -S localhost:8080 -t /var/www/superproject Specify which php.ini to use (default: none) php -S localhost:8080 -c php.ini-development

Slide 27

Slide 27 text

Webserver Examples Use a routing file (example from http://lrnja.net/LigI4U)

Slide 28

Slide 28 text

Session Upload Progress

Slide 29

Slide 29 text

Session Upload Progress File upload progress, written to the session at intervals Useful for user feedback, e.g. shiny new HTML5 progress bars!

Slide 30

Slide 30 text

Tracking Upload Progress User starts uploading file in the usual way " value="123" /> File:

Slide 31

Slide 31 text

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) { ... } } } }

Slide 32

Slide 32 text

Anonymous Functions, __invoke and the Callable Typehint

Slide 33

Slide 33 text

Typehinting We can typehint in PHP, on: • Classes • Interfaces • Arrays

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Anonymous Functions • Literally functions with no name • More convenient than create_function() Example: $ping = function() { echo "ping!"; }; $ping(); // ping!

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Callable Examples function sparkles(Callable $func) { $func(); return "fairy dust"; } class Butterfly { public function __invoke() { echo "flutter"; } } $bee = new Butterfly(); echo sparkles($bee); // flutterfairy dust

Slide 38

Slide 38 text

JsonSerializable

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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()

Slide 41

Slide 41 text

JsonSerializable Example class gardenObject implements JsonSerializable { public function jsonSerialize() { unset($this->herbs); return $this; } } $garden = new gardenObject(); $garden->flowers = array("clematis", "geranium", "hydrangea"); $garden->herbs = array("mint", "sage", "chives", "rosemary"); $garden->fruit = array("apple", "rhubarb"); echo json_encode($garden); {"flowers":["clematis","geranium","hydrangea"],"fruit" :["apple","rhubarb"]}

Slide 42

Slide 42 text

No More Nonsense

Slide 43

Slide 43 text

Removed Features • register_globals • register_long_arrays • safe_mode • magic_quotes • allow_call_time_pass_reference • y2k_compliance • ereg* functions

Slide 44

Slide 44 text

Upgrading to PHP 5.4 • Turn on E_DEPRECATED in PHP 5.3 • Warns when you use features which are gone in 5.4 • Test with the PHP 5.4 webserver

Slide 45

Slide 45 text

PHP 5.4

Slide 46

Slide 46 text

Did I Mention it’s Faster?

Slide 47

Slide 47 text

The PHP 5.4 Features I’ll Actually Use • Short array syntax • Traits • Built in webserver • Upload progress • Callable • JsonSerializable

Slide 48

Slide 48 text

Questions?

Slide 49

Slide 49 text

Resources All in a single bundle for you: http://lrnja.net/KOouXv

Slide 50

Slide 50 text

Thanks! https://joind.in/6937 @lornajane http://lornajane.net