Академический Документы
Профессиональный Документы
Культура Документы
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
$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] => 4
[1] => 19
[2] => 22
[3] => 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
[1] => 8
[2] => 27
[3] => 64
[4] => 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
$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;
?>
- 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";
}
}
Example::__invoke
Static Binding
Early Static Binding
<?php
class Base
{
public static function a()
{
print __METHOD__ . "\n";
self::b();
}
Child::a();
?>
Base::a
Base::b
Static Binding
Late Static Binding
<?php
class Base
{
public static function a()
{
print __METHOD__ . "\n";
static::b();
}
Child::a();
?>
Base::a
Child::b
Namespaces
Declaration
a.php
<?php
namespace project;
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;
c.php
<?php
require 'a.php';
use project\SomeClass;
c.php
<?php
class SomeClass {}
require 'a.php';
use project\SomeClass;
?>
d.php
<?php
class SomeClass {}
require 'a.php';