PHP 5.

3: New Language Features
Sebastian Bergmann
July 14th  2009

Who I am

Sebastian Bergmann Involved in the PHP project since 2000 Creator of PHPUnit Co-Founder and Principal Consultant with thePHP.cc

PHP 5.3
New Language Features

Lambda Functions Closures Functors Late Static Binding Namespaces goto

 \ 

PHP 5.3
Other Improvements

New extensions

phar, intl, fileinfo

Garbage Collector MySQL Native Driver Improved Windows support

PHP 5.3
Performance Improvements

30

20

10

0

PHP 5.0.5

PHP 5.1.6

PHP 5.2.10 PHP 5.3.0

Lambda Functions
In a nutshell

Anonymous functions that
  

are declared on-the-fly, can be assigned to a variable, and passed to other functions

Lambda Functions
Usage
<?php $function = function() { print 'Hello World!'; }; ?>

Lambda Functions
Usage
<?php $function = function() { print 'Hello World!'; }; $function(); ?> Hello World!

Lambda Functions
Usage
<?php $function = function() { print 'Hello World!'; }; call_user_func($function); ?> Hello World!

Lambda Functions
Usage
<?php $function = function() { print 'Hello World!'; }; call_user_func_array($function, array()); ?> Hello World!

Lambda Functions
Usage Scenarios
<?php $list = array(22, 4, 19, 78); usort( $list, function ($a, $b) { if ($a == $b) return 0; return ($a < $b) ? -1 : 1; } ); print_r($list); ?> Array ( [0] [1] [2] [3] )

=> => => =>

4 19 22 78

Lambda Functions
Usage Scenarios
<?php print_r( array_map( function ($n) { return($n * $n * $n); }, array(1, 2, 3, 4, 5) ) ); ?> Array ( [0] [1] [2] [3] [4] )

=> => => => =>

1 8 27 64 125

Lambda Functions
Prior to PHP 5.3
<?php $function = create_function('', 'print "Hello World!";'); $function(); ?> Hello World!

Lambda Functions
Prior to PHP 5.3
<?php $function = create_function('', 'print "Hello World!";'); call_user_func($function); ?> Hello World!

Lambda Functions
Prior to PHP 5.3
<?php $function = create_function('', 'print "Hello World!";'); call_user_func_array($function, array()); ?> Hello World!

Closures
In a nutshell

Anonymous functions that
   

are declared on-the-fly, can be assigned to a variable, passed to other functions, and remember what happens around them

Closures
Lexical Variables
<?php $string = 'Hello World!'; $function = function() use ($string) { print $string; }; $function(); ?> Hello World!

Closures
Lexical Variables
<?php $plus_this = function($num) { return function($arg) use ($num) { return $arg + $num; }; }; print_r( array_map( $plus_this(100), array(1, 2, 3) ) ); ?> Array ( [0] => 101 [1] => 102 [2] => 103 )

Closures
Y-Combinator
<?php function Y($F) { $func = function ($f) { return $f($f); }; return $func( function ($f) use($F) { return $F( function ($x) use($f) { $ff = $f($f); return $ff($x); } ); } );

}

http://en.wikipedia.org/wiki/Fixed_point_combinator

Closures
Lexical Variables with references
<?php $x = 1; $function = function() use ($x) { print $x . "\n"; }; $function(); $x = 2; $function(); print "\n"; $x = 1; $function = function() use (&$x) { print $x . "\n"; }; $function(); $x = 2; $function(); ?> 1 1 1 2

Closures
Reflection API
<?php $function = function($a, $b) { return $a + $b; }; $reflector = new ReflectionFunction($function); print $reflector; ?> Closure [ <user> function {closure} ] { @@ /home/sb/closure_reflection.php 2 - 2 - Parameters [2] { Parameter #0 [ <required> $a ] Parameter #1 [ <required> $b ] } }

Functors
In a nutshell

Allow an object to be invoked or called as if it were an ordinary function Can be used to implement stateful callbacks Also called function objects, functionals or functionoids

Functors
Usage
<?php class Example { public function __invoke() { print __METHOD__ . "\n"; } } $object = new Example; $object(); ?> Example::__invoke

Static Binding
Early Static Binding
<?php class Base { public static function a() { print __METHOD__ . "\n"; self::b(); } public static function b() { print __METHOD__ . "\n"; } } class Child extends Base { public static function b() { print __METHOD__ . "\n"; } } Child::a(); ?> Base::a Base::b

Static Binding
Late Static Binding
<?php class Base { public static function a() { print __METHOD__ . "\n"; static::b(); } public static function b() { print __METHOD__ . "\n"; } } class Child extends Base { public static function b() { print __METHOD__ . "\n"; } } Child::a(); ?> Base::a Child::b

Namespaces
Declaration
a.php <?php namespace project; const ANSWER = 42; class SomeClass {} function do_something() {} ?>

Namespaces
Fully qualified names
a.php <?php namespace project; const ANSWER = 42; class SomeClass {} function do_something() {} ?> b.php <?php require 'a.php'; print project\ANSWER; $object = new project\SomeClass; project\do_something(); ?>

Namespaces
Import from namespace into local scope
a.php <?php namespace project; const ANSWER = 42; class SomeClass {} function do_something() {} ?> c.php <?php require 'a.php'; use project\SomeClass; $object = new SomeClass; ?>

Namespaces
Import from namespace into local scope (conflict)
a.php <?php namespace project; const ANSWER = 42; class SomeClass {} function do_something() {} ?> c.php <?php class SomeClass {} require 'a.php'; use project\SomeClass; ?>
Fatal error: Cannot use project\SomeClass as SomeClass because the name is already in use in /tmp/c.php on line 6

Namespaces
Import from namespace into local scope with alias
a.php <?php namespace project; const ANSWER = 42; class SomeClass {} function do_something() {} ?> d.php <?php class SomeClass {} require 'a.php'; use project\SomeClass as Foo; ?>

The End
Thank you for your interest! These slides will be posted on http://slideshare.net/sebastian_bergmann

License

This presentation material is published under the Attribution-Share Alike 3.0 Unported license. You are free:

to Share – to copy, distribute and transmit the work. to Remix – to adapt the work.

Under the following conditions:

Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

For any reuse or distribution, you must make clear to others the license terms of this work. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights.

Sign up to vote on this title
UsefulNot useful