Slide 1

Slide 1 text

New Wave PHP Lorna Mitchell, PHPUK 2015 (these slides are already online http://lrnja.net/new-wave-php)

Slide 2

Slide 2 text

Recent Versions of PHP

Slide 3

Slide 3 text

New Wave PHP Data from http://w3techs.com

Slide 4

Slide 4 text

Changes in PHP

Slide 5

Slide 5 text

Register Globals is Gone (5.4) Upgrade PHP and see many undefined (simple) variables. To fix each undefined variable: •replace $username with $_GET['username'] or $_POST['username'] in each case

Slide 6

Slide 6 text

Echo Shortcut (5.4) PHP 5.4 removes short_open_tag config option •

Slide 7

Slide 7 text

Error Reporting: E_STRICT E_ALL includes E_STRICT in PHP 5.4+ You can either: 1.Fix your errors 2.Turn it off again using E_ALL & ~E_STRICT

Slide 8

Slide 8 text

Timezone Settings (5.4) PHP refuses to guess the timezone and will warn you. It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. Fix it by setting date.timezone in php.ini: date.timezone = "Europe/London"

Slide 9

Slide 9 text

Call Time Pass By Reference PHP Fatal error: Call-time pass-by-reference has been removed This error says "don't pass references in when the function didn't expect them, you fool"

Slide 10

Slide 10 text

Call Time Pass By Reference You can pass by reference. Declare function: 1 function inspire(&$person) { 2 // be inspiring 3 } Call with just $ and not & 1 inspire($current_user);

Slide 11

Slide 11 text

Traits (5.4) Traits are building blocks for classes Traits can: •build on one another •be used together •are inherited by child classes

Slide 12

Slide 12 text

Traits 1 namespace Silex\Application; 2 use Monolog\Logger; 3 4 trait MonologTrait { 5 public function log($message, $context = array(), $level) { 6 return $this['monolog']->addRecord( 7 $level, $message, $context); 8 } 9 }

Slide 13

Slide 13 text

Traits To use a trait: 1 use Silex\Application; 2 class MyApplication extends Application { 3 use Application\MonologTrait; 4 5 public function myAwesomeMethod() { 6 ... 7 $this->log("Something happened"); 8 ...

Slide 14

Slide 14 text

DateTimeImmutable (5.5) DateTime is awesome 1 $important_date = new DateTime(); 2 print_r($important_date); 3 $result = $important_date->add(new DateInterval('P4D')); 4 print_r($important_date); 5 print_r($result); DateTime Object ( [date] => 2015-02-14 20:28:20 ... ) DateTime Object ( [date] => 2015-02-18 20:28:20 ... ) DateTime Object ( [date] => 2015-02-18 20:28:20 ... )

Slide 15

Slide 15 text

DateTimeImmutable DateTimeImmutable does what you'd expect 1 $important_date = new DateTimeImmutable(); 2 print_r($important_date); 3 $result = $important_date->add(new DateInterval('P4D')); 4 print_r($important_date); 5 print_r($result); DateTime Object ( [date] => 2015-02-14 20:28:20 ... ) DateTime Object ( [date] => 2015-02-14 20:28:20 ... ) DateTime Object ( [date] => 2015-02-18 20:28:20 ... )

Slide 16

Slide 16 text

Easy Password Hashing (5.5) Strongly hash your passwords with a built-in salt. Please? 1 $pass = "secretpassword"; 2 $hashed = password_hash($pass, PASSWORD_DEFAULT); 3 echo $hashed; Output: $2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C63vBSfflQtfjs52GxhAc.s/G (make your password columns wider! 255 chars recommended)

Slide 17

Slide 17 text

Easy Password Hashing 1 $existing_hash = '$2y$10$Q7Rm.Cmcu4lbvI7.C2q4Z.1LLoh4C63vBSfflQt 2 $pass = "secretpassword"; 3 4 if(password_verify($pass, $existing_hash)) 5 echo "All good"; 6 else 7 echo "Go away"; For PHP < 5.5: http://github.com/ircmaxell/password_compat

Slide 18

Slide 18 text

Generators (5.5) Functions with the ability to emit a sequence of values

Slide 19

Slide 19 text

Generators 1 function readLogsFromDatabase() { 2 $db = new PDO('mysql:host=localhost;dbname=test', $user, $p 3 $stmt = $db->query("select message from logs"); 4 while ($row = $stmt->fetch()) { 5 yield $row['message']; 6 } 7 } 8 9 foreach (readLogsFromDatabase() as $msg) { 10 echo $msg."\n"; 11 }

Slide 20

Slide 20 text

Coroutines Coroutines are a way of separating interacting concerns Generators let us achieve this

Slide 21

Slide 21 text

Coroutines 1 function dbLogWriter() { 2 $db = new PDO('mysql:host=localhost;dbname=test'); 3 $stmt = $db->prepare( 4 "insert into logs (message) values (:msg);"); 5 while (true) { 6 $msg = yield; 7 $stmt->execute(array("msg" => $msg)); 8 } 9 }

Slide 22

Slide 22 text

Coroutines 1 $logger = dbLogWriter(); 2 $logger->send("Hello world!"); 3 $logger->send("Something notable happened");

Slide 23

Slide 23 text

MySQL Extension is Deprecated (5.5) The original mysql extension now emits E_DEPRECATED warnings. Instead, use: •PDO - excellent OO database handling •The mysqli extension, quite similar to the original mysql one

Slide 24

Slide 24 text

Built-in OpCache (5.5) Replaces APC - be aware that OpCache is disabled by default. Turn on opcache.enable and opcache.enable_cli

Slide 25

Slide 25 text

DebugInfo (5.6) Magic method __debugInfo() to change what is given to var_dump() 1 class MyBuggyClass { 2 public function __debugInfo() { 3 return $this->problemData; 4 } 5 } 6 7 ... 8 var_dump(new MyBuggyClass()); // custom output

Slide 26

Slide 26 text

Variadic Functions (5.6) 1 function combine($transform, ...$strings) { 2 $string = ''; 3 4 foreach($strings as $piece) { 5 $string .= $piece; 6 } 7 return($transform($string)); 8 } 9 10 echo combine("strtoupper", "I'd ", "like ", 4 + 2, " apples"); 11 // I'D LIKE 6 APPLES

Slide 27

Slide 27 text

Argument Unpacking (5.6) Let PHP unpack your function parameters 1 function display_product($name, $id, $price) {} // cool stuffs 2 3 $db = new PDO("mysql:host=127.0.0.1;dbname=test"); 4 $sql = "select p.name, p.id, p.price from products p"; 5 $stmt = $db->query($sql); 6 7 while(false !== ($prod = $stmt->fetch(PDO::FETCH_NUM))) { 8 display_product(...$prod); 9 }

Slide 28

Slide 28 text

Upgrading An Existing System

Slide 29

Slide 29 text

Performance Comparison

Slide 30

Slide 30 text

How To Upgrade Turn on E_DEPRECATED and watch the logs of your existing platform Set with: error_reporting = E_ALL

Slide 31

Slide 31 text

How To Upgrade Use the PHPCompatibility PHPCS standard Install from https://github.com/wimg/PHPCompatibility and include in your standards directory

Slide 32

Slide 32 text

How To Upgrade Syntax check your code with new version of PHP Use phing (http://www.phing.info/): Or try my one-liner: find . -name '*.php' | xargs -n1 php -l

Slide 33

Slide 33 text

How To Upgrade Compile new PHP and run test suite with it Run tests: /path/to/php/sapi/cli/php phpunit.phar

Slide 34

Slide 34 text

How To Upgrade Run application with PHP's webserver (5.4+) php -S localhost:8080

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

How To Upgrade •Turn on E_DEPRECATED and watch the logs of your existing platform •Use the PHPCompatibility PHPCS standard •Lint check with new version (php -l) •Compile new PHP and run test suite •Run application with PHP's webserver •Upgrade a test/staging platform •Go for it!

Slide 37

Slide 37 text

New Platforms For new projects, or old ones, platforms must be PHP 5.5+

Slide 38

Slide 38 text

Questions? (and resources) Feedback please! https://m.joind.in/talk/c173e Intermediate PHP: •http://lrnja.net/php-video Contact me: •@lornajane •http://lornajane.net

Slide 39

Slide 39 text

Bonus Content

Slide 40

Slide 40 text

Namespaced Functions (5.6) Have always been able to namespace functions Can now import them, like classes 1 include "Lib/Many/Layers/functions.php"; 2 use function \Lib\Many\Layers\deep_func; 3 4 deep_func();

Slide 41

Slide 41 text

Filter Handles MAC Addresses (5.5) Stealth feature (it doesn't seem to be documented!) 1 $mac1 = "12-34-56-78-9A-BC"; 2 var_dump(filter_var($mac1, FILTER_VALIDATE_MAC)); 3 // string(17) "12-34-56-78-9A-BC" 4 5 $mac2 = "12-34-567-A-BC"; 6 var_dump(filter_var($mac2, FILTER_VALIDATE_MAC)); 7 // bool(false)

Slide 42

Slide 42 text

JSON Shininess (5.4) JSONSerializable interface defines the data that is included when object is passed to json_encode() Also very handy additional switches to json_encode(): •JSON_PRETTY_PRINT •JSON_UNESCAPED_SLASHES •JSON_UNESCAPED_UNICODE

Slide 43

Slide 43 text

Reusable php://input (5.6) The php://input stream gives access to the incoming request body From PHP 5.6, it can be opened multiple times