Of Lambda Functions, Closures, and Traits

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.

3 comments

Comments 1 - 3 of 3 previous next Post a comment

  • + greywire greywire 7 months ago
    I guess in my excitement I just blazed right past that slide. :) Considering how long it takes for new versions to come along I guess I cant hold my breath. Is there a way to add this as a patch or an extension or something until then?
  • + sebastian_bergmann Sebastian Bergmann 7 months ago
    Slide 3 clearly states that Traits will be introduced in a release series after PHP 5.3.
  • + greywire greywire 7 months ago
    Are traits going to be in 5.3? I cant seem to find a definitive answer as to whether they will be in 5.3, or if not, what version they will be in.
Post a comment
Embed Video
Edit your comment Cancel

5 Favorites

Of Lambda Functions, Closures, and Traits - Presentation Transcript

  1. Of Lambda Functions, Closures, and Traits Sebastian Bergmann February 27th 2009
  2. Who I Am Sebastian Bergmann  Involved in the PHP  project since 2000 Creator of PHPUnit  Author, Consultant,  Coach, Trainer
  3. New PHP Language Features PHP 5.3  Lambda Functions  Closures  Functors  Namespaces  goto  Future  Traits 
  4. Lambda Functions Anonymous functions that  are declared on-the-fly  can be assigned to a variable  and passed to other functions 
  5. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; ?>
  6. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; $lambda(); ?> Hello World!
  7. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; call_user_func($lambda); ?> Hello World!
  8. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; call_user_func_array($lambda, array()); ?> Hello World!
  9. 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 )
  10. 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 )
  11. Lambda Functions Prior to PHP 5.3 <?php $lambda = create_function('', 'print \"Hello World!\";'); $lambda(); ?> Hello World!
  12. Lambda Functions Prior to PHP 5.3 <?php $lambda = create_function('', 'print \"Hello World!\";'); call_user_func($lambda); ?> Hello World!
  13. Lambda Functions Prior to PHP 5.3 <?php $lambda = create_function('', 'print \"Hello World!\";'); call_user_func_array($lambda, array()); ?> Hello World!
  14. Closures Anonymous functions that  are declared on-the-fly  can be assigned to a variable,  passed to other functions,  and remember what happens around them 
  15. Closures Lexical Variables <?php $string = 'Hello World!'; $closure = function() use ($string) { print $string; }; $closure(); ?> Hello World!
  16. Closures Lexical Variables <?php function getClosure() { $string = 'Hello World!'; return function() use ($string) { print $string; }; } $closure = getClosure(); $closure(); ?> Hello World!
  17. Closures Lexical Variables with reference <?php $x = 1; $closure = function() use ($x) { print $x . \"\\n\"; }; $closure(); $x = 2; $closure(); print \"\\n\"; $x = 1; $closure = function() use (&$x) { print $x . \"\\n\"; }; $closure(); $x = 2; $closure(); ?> 1 1 1 2
  18. Closures Reflection API <?php $closure = function($a, $b) { return $a + $b; }; $reflector = new ReflectionFunction($closure); print $reflector; ?> Closure [ <user> function {closure} ] { @@ /home/sb/closure_reflection.php 2 - 2 - Parameters [2] { Parameter #0 [ <required> $a ] Parameter #1 [ <required> $b ] } }
  19. Functors Allow an object to be invoked or called as if  it were an ordinary function Also called function objects, functionals or  functionoids
  20. Functors Usage <?php class Example { public function __invoke() { print __METHOD__ . \"\\n\"; } } $object = new Example; $object(); ?> Example::__invoke
  21. Traits New unit of reuse  Provide structure, modularity and reusability  within classes Less complex than Multiple Inheritance 
  22. Inheritance Single Inheritance  A class can inherit interface and  implementation from a single parent class
  23. Inheritance Single Inheritance  A class can inherit interface and  implementation from a single parent class Multiple Inheritance  A class can inherit interface and  implementation from multiple parent classes
  24. Inheritance Single Inheritance  A class can inherit interface and  implementation from a single parent class Multiple Inheritance  A class can inherit interface and  implementation from multiple parent classes Interfaces  A class can inherit interface and  implementation from a single parent class A class can implement multiple interfaces 
  25. Inheritance Classes have contradicting goals Generator of Instances  Must be complete  Must have a unique place in the class  hierarchy Unit of Reuse  Should be small  Should be applicable at arbitrary places 
  26. Multiple Inheritance The Diamond Problem class A { Class A declares method()  public function method() { } }
  27. Multiple Inheritance The Diamond Problem class A { Class A declares method() ● public function method() { } Classes B and C inherit from A and  } override method() differently class B extends A { public function method() { } } class C extends A { public function method() { } }
  28. Multiple Inheritance The Diamond Problem class A { Class A declares method() ● public function method() { } Classes B and C inherit from A and ● } override method() differently class B extends A { Class D inherits from both B and C public function method() {  } and does not override method() } class C extends A { public function method() { } } class D extends B, C { }
  29. Multiple Inheritance The Diamond Problem class A { Class A declares method() ● public function method() { } Classes B and C inherit from A and ● } override method() differently class B extends A { Class D inherits from both B and C public function method() { ● } and does not override method() } When we call method() on an  class C extends A { object of D, which implementation public function method() { } should be called? } class D extends B, C { } $d = new D; $d->method();
  30. Traits New unit of reuse  Provide structure, modularity and reusability  within classes Can be incomplete 
  31. Traits New unit of reuse  Provide structure, modularity and reusability  within classes Can be incomplete  Less complex than Multiple Inheritance  The typical problems associated with multiple  inheritance (as well as mixins) are avoided
  32. Traits New unit of reuse  Provide structure, modularity and reusability  within classes Can be incomplete  Less complex than Multiple Inheritance  The typical problems associated with multiple  inheritance (as well as mixins) are avoided Flattening Property  Traits are flattened into classes at compile-  time No notion of traits at runtime 
  33. Traits Lightweight  Stateless  Flexible composition of behaviour  (methods) into classes Overcome some limitations of single inheritance by enabling the reuse of method sets in several independent classes across different class hierarchies Existing implementations  Perl 6, Squeak, Scala, Self, Slate, Fortress
  34. Traits Example <?php <?php trait T { class B extends A { public function method() { public function method() { } } } } class B extends A { class D extends C { Flattening use T; public function method() { } } } class D extends C { ?> use T; } ?>
  35. Traits Classes can implement multiple interfaces  can extend a single parent class  can use multiple traits  Traits can be composed from other traits  can express requirements to the using class  via the abstract mechanism
  36. The End Thank you for your interest! These slides will be linked soon from http://sebastian-bergmann.de/ You can vote for this talk on http://joind.in/163
  37. 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, 9 months ago

custom

1824 views, 5 favs, 2 embeds more stats

More info about this document

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Go to text version

  • Total Views 1824
    • 1789 on SlideShare
    • 35 from embeds
  • Comments 3
  • Favorites 5
  • Downloads 19
Most viewed embeds
  • 34 views on http://disole.wordpress.com
  • 1 views on http://tpno.blogspot.com

more

All embeds
  • 34 views on http://disole.wordpress.com
  • 1 views on http://tpno.blogspot.com

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