SlideShare a Scribd company logo
Lambda Functions & Closures A Sydney PHP Group Presentation 2 nd  October 2008 By Timothy Chandler
Lambda Functions – Lambda Calculus Lambda functions originate from lambda calculus  which was introduced by Alonzo Church and Stephen Cole Kleene in the 1930s. The lambda calculus can be thought of as an idealized, minimalistic programming language.  It is capable of expressing any algorithm , and it is this fact that makes the model of functional programming an important one. The lambda calculus provides the model for functional programming.  Modern functional languages can be viewed as embellishments to the lambda calculus.
Lambda Functions – Implementations Implementing the lambda calculus on a computer involves  treating "functions" as “first-class objects” , which raises implementation issues for stack-based programming languages. This is known as the  Funarg problem  – More on this later. Many languages implement lambda functions. These include: Python C++ C# (2 different implementations – Second one improved in C# v3.0) JavaScript ...and many more...
Lambda Functions – Implementation Examples Python: C++: s Python Lambda Function func  =   lambda  x :  x  **   2 s C++ Lambda Function std :: for_each ( c . begin (),  c . end (),   std :: cout   <<  _1  *  _1  <<   std :: endl ) ;
Lambda Functions – Implementation Examples C#: C# v3.0: s C# v3.0 Lambda Function //Create an delegate instance MathDelegate lambdaFunction  =  i  =>  i  *  i ; Execute ( lambdaFunction ) ; s C# Lambda Function //Declare a delegate signature delegate   double  MathDelegate ( double  i ) ; //Create a delegate instance MathDelegate lambdaFunction  =   delegate ( double  i )   {   return  Math . Pow ( i ,   2 ) ;   }; /* Passing ' lambdaFunction ' function variable to another method, executing,   and returning the result of the function   */ double  Execute ( MathDelegate lambdaFunction )   {   return  lambdaFunction ( 100 ) ;   }
Lambda Functions – Implementation Examples JavaScript: s JavaScript Lambda Function var  lambdaFunction = function ( x )   {   return  x * 10 ; } document.write ( lambdaFunction ( 100 )) ;
Lambda Functions – The PHP Way PHP 5.3: Syntax: s PHP 5.3 Lambda Function <?php $lambdaFunction = function ( $x )   { return  $x * 10 ; }; print  $lambdaFunction ( 100 ) ; ?>   s Lambda Function Syntax function   &   ( parameters )   use   ( lexical vars )   {  body  };
Lambda Functions – The PHP Way The goal of PHP’s Lambda function implementation is to allow for the creation of quick throw-away functions. Don’t confuse with “create_function()”. These functions compile at “run-time”. These functions  DO NOT  compile at “compile-time”. Optcode caches  CANNOT  cache them. Bad practice.
Closures
Closures – The Funarg Problem Lambda functions  MUST  be first-class objects. Funarg, meaning “functional argument”, is a problem in computer science where a “stack-based programming language” has difficulty implementing functions as “first-class objects”. The problem is when the body of a function refers to a variable from the environment that it was created but not the environment of the function call. Standard Solutions: Forbid such references. Create closures.
Closures – The PHP Funarg Problem Solution PHP  5.3  introduces a new keyword ‘use’. Use this new keyword when creating a lambda function to define what variables to import into the lambda functions scope –  This creates a Closure .
Closures – The “use” Keyword Example: Result: s Lambda Function Closure $config = array ( 'paths' => array ( 'examples' => 'c:/php/projects/examples/' )) ; $fileArray = array ( 'example1.php' , 'example2.php' , 'exampleImage.jpg' ) ; $setExamplesPath = function ( $file )   use ( $config )   { return  $config [ 'paths' ][ 'examples' ]. $file ; }; print_r ( array_map ( $setExamplesPath , $fileArray ) ) ; Array   (   [ 0 ]   =>  c : / php / projects / examples / example1 . php [ 1 ]   =>  c : / php / projects / examples / example2 . php [ 2 ]   =>  c : / php / projects / examples / exampleImage . jpg )
Closures – The “use” Keyword Example: Result: s Lambda Function Closure – As an Anonymous Function $config = array ( 'paths' => array ( 'examples' => 'c:/php/projects/examples/' )) ;   $fileArray = array ( 'example1.php' , 'example2.php' , 'exampleImage.jpg' ) ; print_r ( array_map ( function ( $file )   use ( $config ) { return  $config [ 'paths' ][ 'examples' ]. $file ; } , $fileArray )) ;   Array   (   [ 0 ]   =>  c : / php / projects / examples / example1 . php [ 1 ]   =>  c : / php / projects / examples / example2 . php [ 2 ]   =>  c : / php / projects / examples / exampleImage . jpg )
Closures – “use” as reference or copy Variables passed into the “use” block are copied in by default – This is the expected PHP behaviour. You can cause a variable to be imported by reference the same way you do when defining referenced parameters in function declarations. The PHP  5  pass by reference for objects rule still applies.
Closures – “use” by reference Example: Why? Able to directly affect the variable from within the lambda function. If used with a large array, can prevent massive overheads. Memory efficient. s Referenced Variable Import
Lifecycle A lambda function can be created at any point in your application,  except in class declarations . Example: Throws Error: s Lambda Function in Class Declaration class  foo { public  $lambda = function () { return   'Hello World' ; }; public   function   __construct () { print  $this -> lambda () ; } } new  foo () ;   Parse error : syntax error, unexpected T_FUNCTION in  D:\Development\www\php5.3\lambda\5.php  on line  4
Lambda functions can live longer than whatever created them. Example: Result: Lifecycle s Lifecycle Example 1 class  foo { public  $lambda = null ; public   function   __construct () { $ this -> lambda = function ()  { return   'Hello World' ;}; } } $foo = new  foo () ; var_dump ( $foo ) ; $lambda = $foo -> lambda ; unset ( $foo ) ; var_dump ( $foo ) ; print  $lambda () ;   object(foo)#1 (1) { [&quot;lambda&quot;]=> object(Closure)#2 (0) { } } NULL Hello World
Imported variables can also live longer. Example: Result: Lifecycle s Lifecycle Example 2 //Create prefix say function. $say = function ( $prefix ) { return   function ( $suffix )   use (& $prefix ) { print  $prefix . $suffix ; }; }; //Create suffix say function - will loose $prefix right? $say = $say ( 'Hello ' ) ; //Wrong! - Execute new say concatenated function. $say ( 'World!' ) ;   //Outputs &quot;Hello World!&quot; <$prefix><$suffix>   Hello World
Methods and properties used in a closure can live longer than the object. Example: Result: Lifecycle  – Objects s Lifecycle Example 3 class  foo { public  $bar = &quot;Bar \r\n &quot; ; public   function   __construct () { print   &quot;__construct() \r\n “ ;} public   function  __destruct () { print   &quot;__destruct() \r\n “ ;} public   function  getBarLambda () { return   function () { return  $ this -> bar ;}; } } $foo = new  foo () ; $bar = $foo -> getBarLambda () ; print  $bar () ; unset ( $foo ) ; var_dump ( $foo ) ; print  $bar () ; unset ( $bar ) ; print  $bar () ;   __construct() Bar NULL Bar __destruct() Fatal error Function name must be a string in D:\Development\www\php5.3\lambda\8.php on line 31
If a closure exists with a reference to an object’s method or property, that object is not completely destroyed when unset. __destruct() is  NOT  called until the closure is  destroyed . The unset object  CANNOT  be used in this situation as it will be  considered a null value  by anything trying to access it outside the closure environment. Lifecycle  – Objects
Lambda Functions are Closures because they automatically get bound to the scope of the class that they are created in. $this is not always needed in the scope. Removing $this can save on memory. You can block this behaviour by declaring the Lambda Function as static. Object Orientation
Object Orientation Example: Result: s Static Lambda Functions class  foo { public   function  getLambda () { return   function () { var_dump ( $ this ) ;}; } public   function  getStaticLambda () { return   static   function () { var_dump ( $ this ) ;}; } } $foo = new  foo () ; $lambda = $foo -> getLambda () ; $staticLambda = $foo -> getStaticLambda () ; $lambda () ; $staticLambda () ;   object(foo)#1 (0) { } NULL
PHP  5.3  introduces a new magic method. Invokable objects are now possible through the use of the __invoke() magic method. Essentially makes the object a closure. Object Orientation
Object Orientation Example: Result: s Invokable Objects class  foo { public   function  __invoke () { print   'Hello World' ; } } $foo = new  foo ; $foo () ; Hello World
Questions?
Thank you. References http://en.wikipedia.org/wiki/Lambda_calculus http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Funarg_problem http://wiki.php.net/rfc/closures

More Related Content

PPT
3 Function Overloading
PPTX
Arrays &amp; functions in php
PDF
2nd puc computer science chapter 8 function overloading
PPTX
Virtual function in C++ Pure Virtual Function
PDF
Client sidescripting javascript
PPS
Master in javascript
PPTX
Journey of a C# developer into Javascript
PDF
Object Oriented PHP - PART-1
3 Function Overloading
Arrays &amp; functions in php
2nd puc computer science chapter 8 function overloading
Virtual function in C++ Pure Virtual Function
Client sidescripting javascript
Master in javascript
Journey of a C# developer into Javascript
Object Oriented PHP - PART-1

What's hot (20)

PPTX
Compile time polymorphism
PDF
Functional Java 8 in everyday life
PDF
Functional Javascript
PPSX
DIWE - Programming with JavaScript
PPTX
Virtual function
PDF
JavaScript Functions
PDF
OSCON - ES6 metaprogramming unleashed
PDF
Virtual Functions
PDF
20160520 what youneedtoknowaboutlambdas
PPTX
Function overloading in c++
PPTX
Presentation on overloading
PPTX
virtual function
PPTX
Inline function
PPT
Ruby For Java Programmers
PPTX
Things about Functional JavaScript
PPT
C++: inheritance, composition, polymorphism
PPT
Pxb For Yapc2008
PPT
PPTX
PHP: GraphQL consistency through code generation
PPTX
Oop2010 Scala Presentation Stal
Compile time polymorphism
Functional Java 8 in everyday life
Functional Javascript
DIWE - Programming with JavaScript
Virtual function
JavaScript Functions
OSCON - ES6 metaprogramming unleashed
Virtual Functions
20160520 what youneedtoknowaboutlambdas
Function overloading in c++
Presentation on overloading
virtual function
Inline function
Ruby For Java Programmers
Things about Functional JavaScript
C++: inheritance, composition, polymorphism
Pxb For Yapc2008
PHP: GraphQL consistency through code generation
Oop2010 Scala Presentation Stal
Ad

Viewers also liked (20)

PPTX
ECONOMIC LOAD DISPATCH USING PARTICLE SWARM OPTIMIZATION
PPT
PHP - Introduction to PHP - Mazenet Solution
PPT
Unix Master
PDF
Appendex g
PPTX
Ch2(working with forms)
PDF
PDF
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
PPTX
Chapter 5 Input
PPT
PHP - Introduction to PHP Date and Time Functions
PPT
Chapter 4 Form Factors & Power Supplies
PPTX
CIS 110 Chapter 1 Intro to Computers
PPT
Introduction to PHP
PDF
Aquifer Storage and Recovery, Kelley Neumann
PPT
Chapter 1Into the Internet
PPT
Chapter 7 Multimedia
PDF
Appendex a
PPT
Chapter 10 Synchronous Communication
PPT
Chapter 4 Form Factors Power Supplies
KEY
Intermediate PHP
PDF
Appendex b
ECONOMIC LOAD DISPATCH USING PARTICLE SWARM OPTIMIZATION
PHP - Introduction to PHP - Mazenet Solution
Unix Master
Appendex g
Ch2(working with forms)
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
Chapter 5 Input
PHP - Introduction to PHP Date and Time Functions
Chapter 4 Form Factors & Power Supplies
CIS 110 Chapter 1 Intro to Computers
Introduction to PHP
Aquifer Storage and Recovery, Kelley Neumann
Chapter 1Into the Internet
Chapter 7 Multimedia
Appendex a
Chapter 10 Synchronous Communication
Chapter 4 Form Factors Power Supplies
Intermediate PHP
Appendex b
Ad

Similar to PHP 5.3 Part 2 - Lambda Functions & Closures (20)

PPT
Php Reusing Code And Writing Functions
PPTX
Licão 13 functions
PPT
Go OO! - Real-life Design Patterns in PHP 5
PPTX
Introducing PHP Latest Updates
PPTX
New features in jdk8 iti
PDF
Wien15 java8
PPT
PPTX
Functional Programming Concepts for Imperative Programmers
PDF
[2015/2016] JavaScript
PPT
eXo SEA - JavaScript Introduction Training
PDF
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
PPT
course slides -- powerpoint
PPTX
OOPS Object oriented Programming PPT Tutorial
PDF
Integrating Flex And Rails With Ruby Amf
PDF
Flex With Rubyamf
PPT
PHP Fuctions.ppt,IT CONTAINS PHP INCLUDE FUNCTION
PDF
Functional programming in java 8 by harmeet singh
PDF
Elements of Functional Programming in PHP
PPTX
Eclipse Day India 2015 - Java 8 Overview
PDF
Scala is java8.next()
Php Reusing Code And Writing Functions
Licão 13 functions
Go OO! - Real-life Design Patterns in PHP 5
Introducing PHP Latest Updates
New features in jdk8 iti
Wien15 java8
Functional Programming Concepts for Imperative Programmers
[2015/2016] JavaScript
eXo SEA - JavaScript Introduction Training
documents.pub_new-features-in-java-8-it-jpoialjavanaitedwien15java8pdf-java-8...
course slides -- powerpoint
OOPS Object oriented Programming PPT Tutorial
Integrating Flex And Rails With Ruby Amf
Flex With Rubyamf
PHP Fuctions.ppt,IT CONTAINS PHP INCLUDE FUNCTION
Functional programming in java 8 by harmeet singh
Elements of Functional Programming in PHP
Eclipse Day India 2015 - Java 8 Overview
Scala is java8.next()

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
KodekX | Application Modernization Development
PDF
Encapsulation_ Review paper, used for researhc scholars
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Spectral efficient network and resource selection model in 5G networks
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KodekX | Application Modernization Development
Encapsulation_ Review paper, used for researhc scholars

PHP 5.3 Part 2 - Lambda Functions & Closures

  • 1. Lambda Functions & Closures A Sydney PHP Group Presentation 2 nd October 2008 By Timothy Chandler
  • 2. Lambda Functions – Lambda Calculus Lambda functions originate from lambda calculus which was introduced by Alonzo Church and Stephen Cole Kleene in the 1930s. The lambda calculus can be thought of as an idealized, minimalistic programming language. It is capable of expressing any algorithm , and it is this fact that makes the model of functional programming an important one. The lambda calculus provides the model for functional programming. Modern functional languages can be viewed as embellishments to the lambda calculus.
  • 3. Lambda Functions – Implementations Implementing the lambda calculus on a computer involves treating &quot;functions&quot; as “first-class objects” , which raises implementation issues for stack-based programming languages. This is known as the Funarg problem – More on this later. Many languages implement lambda functions. These include: Python C++ C# (2 different implementations – Second one improved in C# v3.0) JavaScript ...and many more...
  • 4. Lambda Functions – Implementation Examples Python: C++: s Python Lambda Function func = lambda x : x ** 2 s C++ Lambda Function std :: for_each ( c . begin (), c . end (), std :: cout << _1 * _1 << std :: endl ) ;
  • 5. Lambda Functions – Implementation Examples C#: C# v3.0: s C# v3.0 Lambda Function //Create an delegate instance MathDelegate lambdaFunction = i => i * i ; Execute ( lambdaFunction ) ; s C# Lambda Function //Declare a delegate signature delegate double MathDelegate ( double i ) ; //Create a delegate instance MathDelegate lambdaFunction = delegate ( double i ) { return Math . Pow ( i , 2 ) ; }; /* Passing ' lambdaFunction ' function variable to another method, executing, and returning the result of the function */ double Execute ( MathDelegate lambdaFunction ) { return lambdaFunction ( 100 ) ; }
  • 6. Lambda Functions – Implementation Examples JavaScript: s JavaScript Lambda Function var lambdaFunction = function ( x ) { return x * 10 ; } document.write ( lambdaFunction ( 100 )) ;
  • 7. Lambda Functions – The PHP Way PHP 5.3: Syntax: s PHP 5.3 Lambda Function <?php $lambdaFunction = function ( $x ) { return $x * 10 ; }; print $lambdaFunction ( 100 ) ; ?> s Lambda Function Syntax function & ( parameters ) use ( lexical vars ) { body };
  • 8. Lambda Functions – The PHP Way The goal of PHP’s Lambda function implementation is to allow for the creation of quick throw-away functions. Don’t confuse with “create_function()”. These functions compile at “run-time”. These functions DO NOT compile at “compile-time”. Optcode caches CANNOT cache them. Bad practice.
  • 10. Closures – The Funarg Problem Lambda functions MUST be first-class objects. Funarg, meaning “functional argument”, is a problem in computer science where a “stack-based programming language” has difficulty implementing functions as “first-class objects”. The problem is when the body of a function refers to a variable from the environment that it was created but not the environment of the function call. Standard Solutions: Forbid such references. Create closures.
  • 11. Closures – The PHP Funarg Problem Solution PHP 5.3 introduces a new keyword ‘use’. Use this new keyword when creating a lambda function to define what variables to import into the lambda functions scope – This creates a Closure .
  • 12. Closures – The “use” Keyword Example: Result: s Lambda Function Closure $config = array ( 'paths' => array ( 'examples' => 'c:/php/projects/examples/' )) ; $fileArray = array ( 'example1.php' , 'example2.php' , 'exampleImage.jpg' ) ; $setExamplesPath = function ( $file ) use ( $config ) { return $config [ 'paths' ][ 'examples' ]. $file ; }; print_r ( array_map ( $setExamplesPath , $fileArray ) ) ; Array ( [ 0 ] => c : / php / projects / examples / example1 . php [ 1 ] => c : / php / projects / examples / example2 . php [ 2 ] => c : / php / projects / examples / exampleImage . jpg )
  • 13. Closures – The “use” Keyword Example: Result: s Lambda Function Closure – As an Anonymous Function $config = array ( 'paths' => array ( 'examples' => 'c:/php/projects/examples/' )) ; $fileArray = array ( 'example1.php' , 'example2.php' , 'exampleImage.jpg' ) ; print_r ( array_map ( function ( $file ) use ( $config ) { return $config [ 'paths' ][ 'examples' ]. $file ; } , $fileArray )) ; Array ( [ 0 ] => c : / php / projects / examples / example1 . php [ 1 ] => c : / php / projects / examples / example2 . php [ 2 ] => c : / php / projects / examples / exampleImage . jpg )
  • 14. Closures – “use” as reference or copy Variables passed into the “use” block are copied in by default – This is the expected PHP behaviour. You can cause a variable to be imported by reference the same way you do when defining referenced parameters in function declarations. The PHP 5 pass by reference for objects rule still applies.
  • 15. Closures – “use” by reference Example: Why? Able to directly affect the variable from within the lambda function. If used with a large array, can prevent massive overheads. Memory efficient. s Referenced Variable Import
  • 16. Lifecycle A lambda function can be created at any point in your application, except in class declarations . Example: Throws Error: s Lambda Function in Class Declaration class foo { public $lambda = function () { return 'Hello World' ; }; public function __construct () { print $this -> lambda () ; } } new foo () ; Parse error : syntax error, unexpected T_FUNCTION in D:\Development\www\php5.3\lambda\5.php on line 4
  • 17. Lambda functions can live longer than whatever created them. Example: Result: Lifecycle s Lifecycle Example 1 class foo { public $lambda = null ; public function __construct () { $ this -> lambda = function () { return 'Hello World' ;}; } } $foo = new foo () ; var_dump ( $foo ) ; $lambda = $foo -> lambda ; unset ( $foo ) ; var_dump ( $foo ) ; print $lambda () ; object(foo)#1 (1) { [&quot;lambda&quot;]=> object(Closure)#2 (0) { } } NULL Hello World
  • 18. Imported variables can also live longer. Example: Result: Lifecycle s Lifecycle Example 2 //Create prefix say function. $say = function ( $prefix ) { return function ( $suffix ) use (& $prefix ) { print $prefix . $suffix ; }; }; //Create suffix say function - will loose $prefix right? $say = $say ( 'Hello ' ) ; //Wrong! - Execute new say concatenated function. $say ( 'World!' ) ; //Outputs &quot;Hello World!&quot; <$prefix><$suffix> Hello World
  • 19. Methods and properties used in a closure can live longer than the object. Example: Result: Lifecycle – Objects s Lifecycle Example 3 class foo { public $bar = &quot;Bar \r\n &quot; ; public function __construct () { print &quot;__construct() \r\n “ ;} public function __destruct () { print &quot;__destruct() \r\n “ ;} public function getBarLambda () { return function () { return $ this -> bar ;}; } } $foo = new foo () ; $bar = $foo -> getBarLambda () ; print $bar () ; unset ( $foo ) ; var_dump ( $foo ) ; print $bar () ; unset ( $bar ) ; print $bar () ; __construct() Bar NULL Bar __destruct() Fatal error Function name must be a string in D:\Development\www\php5.3\lambda\8.php on line 31
  • 20. If a closure exists with a reference to an object’s method or property, that object is not completely destroyed when unset. __destruct() is NOT called until the closure is destroyed . The unset object CANNOT be used in this situation as it will be considered a null value by anything trying to access it outside the closure environment. Lifecycle – Objects
  • 21. Lambda Functions are Closures because they automatically get bound to the scope of the class that they are created in. $this is not always needed in the scope. Removing $this can save on memory. You can block this behaviour by declaring the Lambda Function as static. Object Orientation
  • 22. Object Orientation Example: Result: s Static Lambda Functions class foo { public function getLambda () { return function () { var_dump ( $ this ) ;}; } public function getStaticLambda () { return static function () { var_dump ( $ this ) ;}; } } $foo = new foo () ; $lambda = $foo -> getLambda () ; $staticLambda = $foo -> getStaticLambda () ; $lambda () ; $staticLambda () ; object(foo)#1 (0) { } NULL
  • 23. PHP 5.3 introduces a new magic method. Invokable objects are now possible through the use of the __invoke() magic method. Essentially makes the object a closure. Object Orientation
  • 24. Object Orientation Example: Result: s Invokable Objects class foo { public function __invoke () { print 'Hello World' ; } } $foo = new foo ; $foo () ; Hello World
  • 26. Thank you. References http://en.wikipedia.org/wiki/Lambda_calculus http://en.wikipedia.org/wiki/Closure_(computer_science) http://en.wikipedia.org/wiki/Funarg_problem http://wiki.php.net/rfc/closures

Editor's Notes

  • #2: Do Introduction