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.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    4 Favorites

    Of Lambda Functions, Closures, and Traits - Presentation Transcript

    1. Welcome! Of Lambda Functions, Closures and Traits Sebastian Bergmann http://sebastian-bergmann.de/ th October 28 2008
    2. Who I am ● Sebastian Bergmann ● Involved in the PHP Project since 2000 ● Developer of PHPUnit ● Author, Consultant, Coach, Trainer
    3. New PHP Language Features ● PHP 5.3 – Lambda Functions – Closures – Namespaces ︀ ● Next version (PHP 5.4 or PHP 6.0) – Traits – Grafts
    4. New PHP Language Features ● PHP 5.3 – Lambda Functions – Closures \\ Namespaces ︀ ● Next version (PHP 5.4 or PHP 6.0) – Traits – Grafts
    5. Lambda Functions
    6. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; ?>
    7. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; $lambda(); ?> Hello World!
    8. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; call_user_func($lambda); ?> Hello World!
    9. Lambda Functions Usage <?php $lambda = function() { print 'Hello World!'; }; call_user_func_array($lambda, array()); ?> Hello World!
    10. 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 )
    11. Lambda Functions Usage Scenarios <?php print_r( array_map( function ($n) { return($n * $n * $n); }, array(1, 2, 3, 4, 5) ) ); ?> More about array_map() and array_reduce() tomorrow: Map, Filter, Reduce: In the Small and in the Cloud Array ( [0] => 1 [1] => 8 [2] => 27 [3] => 64 [4] => 125 )
    12. Lambda Functions Prior to PHP 5.3 <?php $lambda = create_function('', 'print \"Hello World!\";'); $lambda(); ?> Hello World!
    13. Lambda Functions Prior to PHP 5.3 <?php $lambda = create_function('', 'print \"Hello World!\";'); call_user_func($lambda); ?> Hello World!
    14. Lambda Functions Prior to PHP 5.3 <?php $lambda = create_function('', 'print \"Hello World!\";'); call_user_func_array($lambda, array()); ?> Hello World!
    15. Closures
    16. Closures What is a closure? In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. This slide contains material by Stuart Langridge
    17. Closures What is a closure? Dictionary This slide contains material by Stuart Langridge
    18. Closures What is a closure? A closure is a function that – is anonymous, just like a lambda function – remembers what happens around it This slide contains material by Stuart Langridge
    19. Closures Lexical Variables <?php $string = 'Hello World!'; $closure = function() use ($string) { print $string; }; $closure(); ?> Hello World!
    20. Closures Lexical Variables with reference <?php $x = 1; $closure = function() use (&$x) { $x++; }; $closure(); print $x; ?> 2
    21. Closures $this as a lexical variable <?php class Example { public function one() { print \"Example::one()\\n\"; $closure = function() { $this->two(); }; $closure(); } public function two() { print \"Example::two()\\n\"; } } $object = new Example; $object->one(); ?> Example::one() Example::two()
    22. Closures Reflection API <?php $closure = function($a, $b) { return $a + $b; }; $reflector = new ReflectionMethod($closure, '__invoke'); print $reflector; ?> Method [ <internal> public method __invoke ] { - Parameters [2] { Parameter #0 [ <required> $a ] Parameter #1 [ <required> $b ] } }
    23. __invoke() <?php class Example { public function __invoke() { print 'Example::__invoke()'; } } $object = new Example; $object(); ?> Example::__invoke()
    24. Traits
    25. Inheritance ● Single Inheritance – A class can inherit interface and implementation from a single parent class
    26. Inheritance ● Single Inheritance – A class can inherit interface and implementation from a single parent class ● Interfaces – A class can inherit interface and implementation from a single parent class – A class can implement multiple interfaces
    27. Inheritance ● Single Inheritance – A class can inherit interface and implementation from a single parent class ● Interfaces – A class can inherit interface and implementation from a single parent class – A class can implement multiple interfaces ● Multiple Inheritance – A class can inherit interface and implementation from multiple parent classes
    28. Multiple Inheritance Problems ● A class has 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
    29. Multiple Inheritance Problems ● A class has 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 ● “Multiple inheritance is good, but there is no good way to do it.” (Steve Cook) – Example: The Diamond Problem
    30. Multiple Inheritance The Diamond Problem class A { ● Class A declares method() public function method() { } }
    31. 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() { } }
    32. 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 D inherits from both B and C } and does not override method() } class C extends A { public function method() { } } class D extends B, C { }
    33. 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 D inherits from both B and C } and does not override method() } class C extends A { ● When we call method() on an public function method() { object of D, which implementation } should be called? } class D extends B, C { } $d = new D; $d->method();
    34. Traits ● New unit of reuse – Provide structure, modularity and reusability within classes – Can be incomplete
    35. 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
    36. 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
    37. 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
    38. Traits <?php trait T { public function method() { } } class B extends A { use T; } class D extends C { use T; } ?>
    39. Traits <?php <?php trait T { class B extends A { public function method() { public function method() { } } } } class B extends A { Flattening class D extends C { use T; public function method() { } } } class D extends C { ?> use T; } ?>
    40. 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
    41. The End ● Thank you for your interest! ● These slides will be available shortly on http://sebastian-bergmann.de/talks/.
    42. 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, 2 years ago

    custom

    3553 views, 4 favs, 18 embeds more stats

    Lambda Functions and Closures allow the quick defin more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 3553
      • 2502 on SlideShare
      • 1051 from embeds
    • Comments 0
    • Favorites 4
    • Downloads 55
    Most viewed embeds
    • 498 views on http://sebastian-bergmann.de
    • 331 views on http://www.programania.net
    • 103 views on http://www.planet-php.net
    • 35 views on http://bao.nguyenquoc.com
    • 16 views on http://www.planetacodigo.com

    more

    All embeds
    • 498 views on http://sebastian-bergmann.de
    • 331 views on http://www.programania.net
    • 103 views on http://www.planet-php.net
    • 35 views on http://bao.nguyenquoc.com
    • 16 views on http://www.planetacodigo.com
    • 14 views on http://planet-php.org
    • 12 views on http://static.slideshare.net
    • 11 views on http://www.planet-php.org
    • 10 views on http://www.diegomunoz.cl
    • 6 views on http://www.nqbao.com
    • 5 views on http://lj-toys.com
    • 2 views on http://jemmy.netbeans.org
    • 2 views on http://127.0.0.1:8795
    • 2 views on http://planet-php.net
    • 1 views on http://xss.yandex.net
    • 1 views on http://www.aulanz.net
    • 1 views on applewebdata://D71F30F8-27E1-4DF2-B5FE-8EEA37D34D2B
    • 1 views on http://safe.tumblr.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