Slide 1

Slide 1 text

PHP 5.NEXT The New Bits

Slide 2

Slide 2 text

Proprietary and Confidential •Community Engineer at Engine Yard •Author of Zend PHP 5 Certification Study Guide, Sitepoints PHP Anthology: 101 Essential Tips, Tricks & Hacks & PHP Master: Write Cutting Edge Code •A contributor to Zend Framework 1 & 2, phpdoc, and PHP internals •@dshafik Davey Shafik

Slide 3

Slide 3 text

About These Slides

Slide 4

Slide 4 text

Proprietary and Confidential • Two slides per “slide”! – Title Slide (for when I’m talking)! – Details slide (for later)! • Nobody likes it when you can read the slide just as well as the speaker can! • I like slides that are useful About These Slides

Slide 5

Slide 5 text

The Small Stuff

Slide 6

Slide 6 text

T_POW — The “exponent” operator

Slide 7

Slide 7 text

Proprietary and Confidential • Double Asterisk (**) operator! • Raises the left operator to the right power! • Right Associative T_POW — The “exponent” operator echo 2 ** 3 ** 2; // 512 (not 64) 2 ** (3 ** 2) echo -3 ** 2; // -9 (not 9) -(3 ** 2) echo 1 - 3 ** 2; // -8 (not 4)
 1 - (3 ** 2) echo ~3 ** 2; // -10 (not 16) ~(3 ** 2)

Slide 8

Slide 8 text

Constant Scalar Expressions

Slide 9

Slide 9 text

Proprietary and Confidential • Use expressions when defining:! • global constants with const keyword! • class constants with the const keyword! • class properties! • static variables! • function arguments Constant Scalar Expressions

Slide 10

Slide 10 text

Proprietary and Confidential • ! 123 - Integers! • ! 123.456 - Floats! • ! “foo” - Strings! • ! __LINE__ - Line magic constant! • ! __FILE__ - File magic constant! • ! __DIR__ - Directory magic constant! • ! __TRAIT__ - Trait magic constant! • ! __METHOD__ - Method magic constant! • ! __FUNCTION__ - Function magic constant! • ! __NAMESPACE__ - Namespace magic constant! • ! <<

Slide 11

Slide 11 text

Proprietary and Confidential Global constants with const keyword const FOO = 1 + 1; const BAR = 1 << 1; const GREETING = "HELLO"; const BAZ = GREETING." WORLD!"

Slide 12

Slide 12 text

Proprietary and Confidential Class constants with the const keyword class Foo { const FOO = 1 + 1; const BAR = 1 << 1; const GREETING = "HELLO"; const BAZ = self::GREETING." WORLD!" }

Slide 13

Slide 13 text

Proprietary and Confidential Class Properties class Foo { const BAZ = 10; } class Bar { public $foo = 1 + 1; public $bar = [ 1 + 1, 1 << 2, Foo::BAZ => "foo "."bar" ]; public $baseDir = __DIR__ . "/base"; }

Slide 14

Slide 14 text

Proprietary and Confidential Static Variables const BAR = 0x10; function foo() { static $a = 1 + 1; static $b = [1 << 2]; static $c = 0x01 | BAR; }

Slide 15

Slide 15 text

Proprietary and Confidential Function Arguments const  BAR  =  1;       function  f($a  =  1  +  1,  $b  =  2  <<  3,  $c  =  BAR?10:100)  {   }

Slide 16

Slide 16 text

__debugInfo()

Slide 17

Slide 17 text

Proprietary and Confidential • Magic method to control the output of var_dump() __debugInfo() class File { // "Resource(stream)" isn't all that useful private $fp; // But all the stream meta data is public function __debugInfo() { return $this->fp ? stream_get_meta_data($fp) : []; } public function open($filename, $mode = 'r'){ $this->fp = fopen($filename, $mode); } }

Slide 18

Slide 18 text

Proprietary and Confidential __debugInfo() (Cont.) $f = new File; var_dump($f); // object(File)#1 { } $f->open('http://php.net'); var_dump($f); /* object(File)#1 { ["wrapper_type"]=> string(4) "http" ["stream_type"]=> string(10) "tcp_socket" etc... */

Slide 19

Slide 19 text

GMP Improvements + Operator Overloading

Slide 20

Slide 20 text

Proprietary and Confidential • GMP switches to use objects, instead of resources! • Serializable! • Castable! • var_dump()able! • Operator Overloading! • Internal Only :(! • Allows objects to define the behavior when used as operands with:! • Basic Math Operators: + - * / % • Bitshifting: << >> • Concat: .! • Boolean Operators: | & ^ XOR ~ ! GMP Improvements + Operator Overloading

Slide 21

Slide 21 text

Proprietary and Confidential GMP Improvements + Operator Overloading $result  =  gmp_mod(          gmp_add(                  gmp_mul($c0,  gmp_mul($ms0,  gmp_invert($ms0,  $n0))),                  gmp_add(                          gmp_mul($c1,  gmp_mul($ms1,  gmp_invert($ms1,  $n1))),                          gmp_mul($c2,  gmp_mul($ms2,  gmp_invert($ms2,  $n2)))                  )          ),          gmp_mul($n0,  gmp_mul($n1,  $n2))   );

Slide 22

Slide 22 text

Proprietary and Confidential GMP Improvements + Operator Overloading $result  =  (          $c0  *  $ms0  *  gmp_invert($ms0,  $n0)      +  $c1  *  $ms1  *  gmp_invert($ms1,  $n1)      +  $c2  *  $ms2  *  gmp_invert($ms2,  $n2)   )  %  ($n0  *  $n1  *  $n2);

Slide 23

Slide 23 text

phpdbg SAPI

Slide 24

Slide 24 text

Proprietary and Confidential • GDB-like debugger for PHP! • Stand-alone SAPI, like CLI, CGI, or php-fpm! • Not an extension like xdebug/Zend Debugger! • Full featured! • Break on file, function, method, opline address, current file line #, expressions, conditions, and opcodes! • Full disassembly! • Remote debugging phpdbg SAPI

Slide 25

Slide 25 text

Proprietary and Confidential

Slide 26

Slide 26 text

Import Namespaced Functions & Constants

Slide 27

Slide 27 text

Proprietary and Confidential • Prior to PHP 5.6:! • Outside of the namespace, function names/constants had to be fully qualified! • You could alias the namespace to shorten it (ugh!)! ! • Add two new keyword sequences:! • use function \foo\bar\function! • use const \foo\bar\BAZ! • Use commas to import multiples Import Namespaced Functions & Constants

Slide 28

Slide 28 text

Proprietary and Confidential Import Namespaced Functions & Constants namespace foo\bar { const HELLO_WORLD = "Hello World"; function strlen($str) { return \strlen($str); } } namespace { use function foo\bar\strlen; use function foo\bar\non_existent; // Doesn't exist! use const foo\bar\HELLO_WORLD; var_dump(strlen(HELLO_WORLD)); // not ambiguous var_dump(non_existent()); // Does not fall-through // to global scope: fatal error }

Slide 29

Slide 29 text

Proprietary and Confidential • Great for DSLs! Import Namespaced Functions & Constants use function html\div, html\p, html\em; $html = div(p('Some', em('Text')));

Slide 30

Slide 30 text

Variadic Functions

Slide 31

Slide 31 text

Variadic Syntax … $variable

Slide 32

Slide 32 text

Proprietary and Confidential • Variadic means to accept a variable number of arguments! • Previously you needed to use func_num_args()/func_get_args()! • Which includes defined arguments also!! • New syntax: "… $variable" (triple dot)! • Array with just the variable arguments! • Self-documenting code! • Supports by-reference! • Allows type hints! • Must be the last argument! Variadic Functions

Slide 33

Slide 33 text

Variadic Functions function fn($reqParam, $optParam = null, ...$params) { var_dump($reqParam, $optParam, $params); } fn(1); // 1, null, [] fn(1, 2); // 1, 2, [] fn(1, 2, 3); // 1, 2, [3] fn(1, 2, 3, 4); // 1, 2, [3, 4] fn(1, 2, 3, 4, 5); // 1, 2, [3, 4, 5]

Slide 34

Slide 34 text

Variadic Functions — References class MySQL implements DB { public function prepare($query, &...$params) { $stmt = $this->pdo->prepare($query); foreach ($params as $i => &$param) { $stmt->bindParam($i + 1, $param); } return $stmt; } // ... } $stmt = $db->prepare('INSERT INTO users 
 (name, email, age) VALUES (?, ?, ?)', $name, $email, $age);
 foreach ($usersToInsert as list($name, $email, $age)) { $stmt->execute(); }

Slide 35

Slide 35 text

Variadic Functions — Type hints function hooks($name, Callable ... $callbacks) { foreach ($callbacks as $callback) { $callback(); } }

Slide 36

Slide 36 text

Argument Unpack (AKA: SPLAT)

Slide 37

Slide 37 text

Splat Syntax … $variable

Slide 38

Slide 38 text

Splat Syntax function fn(… $variable)

Slide 39

Slide 39 text

Proprietary and Confidential • The opposite of variadics, unpack an array (or similar) as arguments! • No more call_user_func_array() (which is “slow”)! • Valid for any argument list (including new foo(… $var))! • No limitations on placement! • Use it multiple times! Splat

Slide 40

Slide 40 text

Splat function test(...$args) { var_dump($args); } test(1, 2, 3); // [1, 2, 3] test(...[1, 2, 3]); // [1, 2, 3] test(...new ArrayIterator([1, 2, 3])); // [1, 2, 3]

Slide 41

Slide 41 text

Splat $args1 = [1, 2, 3]; $args2 = [4, 5, 6]; test(...$args1, ...$args2); // [1, 2, 3, 4, 5, 6] test(1, 2, 3, ...$args2); // [1, 2, 3, 4, 5, 6] test(...$args1, 4, 5, 6); // [1, 2, 3, 4, 5, 6]

Slide 42

Slide 42 text

What Next?

Slide 43

Slide 43 text

HHVM

Slide 44

Slide 44 text

Proprietary and Confidential • Alternative PHP runtime from Facebook! • Super Fast! • Close to feature parity! • Mostly just missing some key extensions! • Crazy Fast (no really) HHVM

Slide 45

Slide 45 text

Proprietary and Confidential • The most un-googleable name for a thing, ever! • A “new” language based on PHP that supports strong type hinting! • Mostly a dev-tool: instant static analysis can detect bugs and other issues during development! • Ignored during runtime! • Uses

Slide 46

Slide 46 text

HHVM: Hack — Type hints $a) { return array_reduce($a, ($sum, $i) ==> $sum + $i); }

Slide 47

Slide 47 text

Proprietary and Confidential • Enhanced/Specialized Arrays! • OO interface for arrays! • Frozen* and Mutable* variants HHVM: Hack — Vectors, Sets, Maps add("a"); $v->reverse(); $v = $v->map(($_) ==> strtoupper($_))->filter(($_) ==> strcmp($_, "D") < 0);

Slide 48

Slide 48 text

HHVM: Hack — Native X(HT)ML add("a"); $v->reverse(); $v = $v->map(($_) ==> strtoupper($_))->filter(($_) ==> strcmp($_, "D") < 0); $items =
    ; foreach ($v as $i) { $items->appendChild(
  • {$i}
  • ); } echo
    Vector:{$items}
    ; // A, B, C $m = Map {"a" => 1, "b" => 2}; $m->add(Pair {"d", 4}); echo
    Map:
    • contains "a": {$m->contains("a") ? "yes" : "no"}
    • // yes
    • contains "c": {$m->contains("c") ? "yes" : "no"}
    • // no
    • size: {$m->count()}
    • // 3
    ;

Slide 49

Slide 49 text

ReactPHP Asynchronous Event-based Applications

Slide 50

Slide 50 text

ReactPHP reactphp.org

Slide 51

Slide 51 text

ReactPHP $app = function ($request, $response) { $response->writeHead(200,['Content-Type' => ‘text/plain']); $response->end("Hello World\n"); }; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket, $loop); $http->on('request', $app); echo "Server running at http://127.0.0.1:8080\n"; $socket->listen(8080); $loop->run(); Uses LibEV, LibEvent or Streams Socket Select

Slide 52

Slide 52 text

Proprietary and Confidential ! • Async Redis Client! • ZeroMQ, Stomp Support! • Promises (CommonJS Promises/A)! • Partials! • GifSocket
 "Real Time communication library using Animated Gifs as a transport™" - Alvaro Videla. ReactPHP

Slide 53

Slide 53 text

Proprietary and Confidential Feedback & Questions: Feedback: https://joind.in/10691
 Twitter: @dshafik Email: [email protected] Slides: http://daveyshafik.com/slides