PHP 5.3: New Language Features

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites & 1 Group

    PHP 5.3: New Language Features - Presentation Transcript

    1. PHP 5.3: New Language Features Sebastian Bergmann July 14th  2009
    2. Who I am  Sebastian Bergmann  Involved in the PHP project since 2000  Creator of PHPUnit  Co-Founder and Principal Consultant with thePHP.cc
    3. PHP 5.3 New Language Features  Lambda Functions  Closures  Functors  Late Static Binding Namespaces  goto
    4. PHP 5.3 Other Improvements  New extensions  phar, intl, fileinfo  Garbage Collector  MySQL Native Driver  Improved Windows support
    5. 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
    6. Lambda Functions In a nutshell Anonymous functions that  are declared on-the-fly,  can be assigned to a variable,  and passed to other functions
    7. Lambda Functions Usage <?php $function = function() { print 'Hello World!'; }; ?>
    8. Lambda Functions Usage <?php $function = function() { print 'Hello World!'; }; $function(); ?> Hello World!
    9. Lambda Functions Usage <?php $function = function() { print 'Hello World!'; }; call_user_func($function); ?> Hello World!
    10. Lambda Functions Usage <?php $function = function() { print 'Hello World!'; }; call_user_func_array($function, array()); ?> Hello World!
    11. 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 )
    12. 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 )
    13. Lambda Functions Prior to PHP 5.3 <?php $function = create_function('', 'print "Hello World!";'); $function(); ?> Hello World!
    14. Lambda Functions Prior to PHP 5.3 <?php $function = create_function('', 'print "Hello World!";'); call_user_func($function); ?> Hello World!
    15. Lambda Functions Prior to PHP 5.3 <?php $function = create_function('', 'print "Hello World!";'); call_user_func_array($function, array()); ?> Hello World!
    16. 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
    17. Closures Lexical Variables <?php $string = 'Hello World!'; $function = function() use ($string) { print $string; }; $function(); ?> Hello World!
    18. 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 )
    19. 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
    20. 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
    21. 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 ] } }
    22. 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
    23. Functors Usage <?php class Example { public function __invoke() { print __METHOD__ . "n"; } } $object = new Example; $object(); ?> Example::__invoke
    24. 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
    25. 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
    26. Namespaces Declaration a.php <?php namespace project; const ANSWER = 42; class SomeClass {} function do_something() {} ?>
    27. Namespaces Fully qualified names a.php <?php namespace project; const ANSWER = 42; class SomeClass {} function do_something() {} ?> b.php <?php require 'a.php'; print projectANSWER; $object = new projectSomeClass; projectdo_something(); ?>
    28. 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 projectSomeClass; $object = new SomeClass; ?>
    29. 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 projectSomeClass; ?> Fatal error: Cannot use projectSomeClass as SomeClass because the name is already in use in /tmp/c.php on line 6
    30. 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 projectSomeClass as Foo; ?>
    31. The End Thank you for your interest! These slides will be posted on http://slideshare.net/sebastian_bergmann
    32. 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.

    + Sebastian BergmannSebastian Bergmann, 4 months ago

    custom

    1018 views, 2 favs, 0 embeds more stats

    More info about this document

    CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

    Go to text version

    • Total Views 1018
      • 1018 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 19
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags

    Groups / Events