Slide 1

Slide 1 text

Pains and Gains of PHP Upgrades Lorna Mitchell, May 2014

Slide 2

Slide 2 text

About Recent Versions of PHP Version Released End of Life 5.2 November 2006 January 2011 5.3 June 2009 July 2014 5.4 March 2012 Release + 3 years 5.5 June 2013 Release + 3 years 5.6 Soon Release + 3 years

Slide 3

Slide 3 text

Current PHP Installations

Slide 4

Slide 4 text

Pains of Upgrades • cost (time and money) • risk

Slide 5

Slide 5 text

Gains of Upgrades • shiny new features • better security • better performance

Slide 6

Slide 6 text

Not All Upgrades Are Equal • to 5.3: climbing a mountain • to 5.4: flying leap • to 5.5: nobody will notice • to 5.6: another anticlimax

Slide 7

Slide 7 text

to PHP 5.3

Slide 8

Slide 8 text

PHP 5.3 is Full of Goodness • namespaces

Slide 9

Slide 9 text

PHP Namespaces Namespaces allow modularity and separation • classes, constants and functions belong in namespaces • namespaces can be nested • much easier pick-and-mix of frameworks/libraries • avoids naming collision • key ingredient for PSR-0

Slide 10

Slide 10 text

Namespaced Library File words[ array_rand($this->words)]; } }

Slide 11

Slide 11 text

Namespaced Calling Code speak(); $other = new \StdClass();

Slide 12

Slide 12 text

PHP 5.3 is Full of Goodness • namespaces • anonymous functions

Slide 13

Slide 13 text

Anonymous Functions "Alice", "colour" => "Blue", "origin" => "Dreams"); function formatit($value, $key) { echo $key . ": " . $value . "\n"; } array_walk($things, "formatit");

Slide 14

Slide 14 text

Anonymous Functions "Alice", "colour" => "Blue", "origin" => "Dreams"); array_walk($things, function ($value, $key) { echo $key . ": " . $value . "\n"; });

Slide 15

Slide 15 text

PHP 5.3 is Full of Goodness • namespaces • anonymous functions • better DateTime • SPL data structures • works on Windows • E_DEPRECATED error reporting level • late static binding • __DIR__

Slide 16

Slide 16 text

Upgrading to 5.3: Gotchas new reserved words: namespace, goto log message changes requires MySQL > 4.1 full info: http://lrnja.net/ZpvknE (php.net)

Slide 17

Slide 17 text

to PHP 5.4

Slide 18

Slide 18 text

PHP 5.4 is a Leap to the Future • improved performance, reduced memory footprint

Slide 19

Slide 19 text

PHP Performance

Slide 20

Slide 20 text

PHP 5.4 is a Leap to the Future • improved performance, reduced memory footprint • built-in webserver

Slide 21

Slide 21 text

PHP's Own Webserver php -S dev.project.local:8080 -t /var/www/cool-project/web -c php-ini.development routing.php

Slide 22

Slide 22 text

PHP 5.4 is a Leap to the Future • improved performance, reduced memory footprint • built-in webserver • traits

Slide 23

Slide 23 text

Simple Traits Example

Slide 24

Slide 24 text

Simple Traits Example getAuditTrail();

Slide 25

Slide 25 text

PHP 5.4 is a Leap to the Future • improved performance, reduced memory footprint • built-in webserver • short array notation, array dereferencing

Slide 26

Slide 26 text

Short Array Notation 'stone', 1 => 'paper', 2 => 'scissors'); $game = [0 => 'scissors', 1 => 'stone', 2 => 'paper'];

Slide 27

Slide 27 text

PHP 5.4 is a Leap to the Future • improved performance, reduced memory footprint • built-in webserver • short array notation, array dereferencing • echo shortcut always available

Slide 28

Slide 28 text

PHP 5.4 Echo Shortcut PHP 5.4 removes short_open_tag config option • A template saying "hi, "

is the same as:

A template saying "hi, "

Slide 29

Slide 29 text

PHP 5.4 is a Leap to the Future • improved performance, reduced memory footprint • built-in webserver • traits • short array notation, array dereferencing • less nonsense

Slide 30

Slide 30 text

Upgrading to PHP 5.4: Gotchas new reserved words: trait callable insteadof no register_globals, short open tag etc turn on E_DEPRECATED on your 5.3 platform compile PHP 5.4 and use the webserver

Slide 31

Slide 31 text

Upgrading to PHP 5.4: Gotchas new reserved words: trait callable insteadof no register_globals, short open tag etc turn on E_DEPRECATED on your 5.3 platform compile PHP 5.4 and use the webserver beware APC issues

Slide 32

Slide 32 text

to PHP 5.5

Slide 33

Slide 33 text

PHP 5.5: New Features • password hashing features

Slide 34

Slide 34 text

Easy Password Hashing

Slide 35

Slide 35 text

Easy Password Hashing

Slide 36

Slide 36 text

PHP 5.5: New Features • password hashing features • generators

Slide 37

Slide 37 text

PHP Generators Simpler than iterators - a function returning a sequence of values.

Slide 38

Slide 38 text

PHP Generators Simpler than iterators - a function returning a sequence of values.

Slide 39

Slide 39 text

PHP 5.5: New Features • password hashing features • generators • try/catch/finally

Slide 40

Slide 40 text

Try/Catch/Finally Finally is a clause that always gets run

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

PHP 5.5: New Features • password hashing features • generators • try/catch/finally • built-in opcode cache • Turn on opcache.enable and opcache.enable_cli • use APCu https://github.com/krakjoe/apcu

Slide 43

Slide 43 text

Upgrading to PHP 5.5 • turn on E_DEPRECATED on your 5.4 platform • compile PHP 5.5 and use the webserver

Slide 44

Slide 44 text

to PHP 5.6

Slide 45

Slide 45 text

PHP 5.6: Just Better • variadic functions

Slide 46

Slide 46 text

Variadic Functions Variable parameters with func_get_args() function concatenate($transform) { $string = ''; $pieces = func_get_args(); $transform = array_shift($pieces); foreach($pieces as $piece) { $string .= $piece; } return($transform($string)); } echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Variadic Functions Much cleaner with new syntax: function concatenate($transform, ...$strings) { $string = ''; foreach($strings as $piece) { $string .= $piece; } return($transform($string)); } echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

PHP 5.6: Just Better • variadic functions • argument unpacking

Slide 51

Slide 51 text

Argument Unpacking Let PHP unpack your args $email[] = "[email protected]"; $email[] = "Hi there"; $email[] = "Thanks for registering, hope you like it"; // old syntax mail($email[0], $email[1], $email[2]); //new syntax mail(...$email);

Slide 52

Slide 52 text

Argument Unpacking Let PHP unpack your args $email[] = "Hi there"; $email[] = "Thanks for registering, hope you like it"; mail("[email protected]", ...$email);

Slide 53

Slide 53 text

PHP 5.6: Just Better • variadic functions • argument unpacking • import namespaced functions/constants

Slide 54

Slide 54 text

Import Namespaced Functions For classes: use \Lib\Many\Layers\Thing; $a = new Thing(); Now also for functions: include "Lib/Many/Layers/functions.php"; use function \Lib\Many\Layers\deep_func; deep_func();

Slide 55

Slide 55 text

PHP 5.6: Just Better • variadic functions • argument unpacking • import namespaced functions/constants • exponentiation operator

Slide 56

Slide 56 text

Exponentiation Operator Like pow() but as an operator echo pow(4,3); // 64 echo 4 ** 3; //64

Slide 57

Slide 57 text

PHP 5.6: Just Better • variadic functions • argument unpacking • import namespaced functions/constants • exponentiation operator • phpdbg included

Slide 58

Slide 58 text

Upgrading to PHP 5.6 • turn on E_DEPRECATED on your 5.5 platform • compile PHP 5.6 and use the webserver

Slide 59

Slide 59 text

In Summary

Slide 60

Slide 60 text

Upgrade, FFS

Slide 61

Slide 61 text

General Upgrade Plan • check the changelog: http://php.net/ChangeLog-5.php • turn on E_DEPRECATED • test with the webserver, run unit tests with new binary • upgrade test platform • pecl extensions will need a reinstall • go for it :)

Slide 62

Slide 62 text

Any Questions? Contact me • @lornajane • http://lornajane.net

Slide 63

Slide 63 text

Appendices

Slide 64

Slide 64 text

PHP Version Adoption • Version 5.3 52.1% • Version 5.2 28.1% • Version 5.4 16.3% • Version 5.5 1.9% • Version 5.1 1.5% (from W3Techs.com, 5 May 2014)

Slide 65

Slide 65 text

PHP Performance Comparisons Average time to run bench.php on my laptop • 5.3.28: 3.92 seconds • 5.4.27: 2.72 seconds • 5.5.11: 2.69 seconds • 5.6.0beta2: 2.36 seconds