Presentation Transcript
PHP 5.3 – Part 2 :PHP 5.3 – Part 2 Lambda Functions & Closures A Sydney PHP Group Presentation
2nd October 2008
By Timothy Chandler
Lambda Functions – Lambda Calculus :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 :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 :Lambda Functions – Implementation Examples Python:
C++:
Lambda Functions – Implementation Examples :Lambda Functions – Implementation Examples C#:
C# v3.0:
Lambda Functions – Implementation Examples :Lambda Functions – Implementation Examples JavaScript:
Lambda Functions – The PHP Way :Lambda Functions – The PHP Way PHP 5.3:
Syntax:
Lambda Functions – The PHP Way :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.
Slide 9: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.
Closures provide a very useful tool for making lambda functions more useful. Closures
Closures – The Funarg Problem :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 :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 :Closures – The “use” Keyword Example:
Result:
Closures – The “use” Keyword :Closures – The “use” Keyword Example:
Result:
Closures – “use” as reference or copy :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 :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.
Lifecycle :Lifecycle A lambda function can be created at any point in your application, except in class declarations.
Example:
Throws Error:
Lifecycle :Lambda functions can live longer than whatever created them.
Example:
Result: Lifecycle
Lifecycle :Imported variables can also live longer.
Example:
Result: Lifecycle
Lifecycle – Objects :Methods and properties used in a closure can live longer than the object.
Example:
Result: Lifecycle – Objects
Lifecycle – Objects :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
Object Orientation :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 :Object Orientation Example:
Result:
Object Orientation :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 :Object Orientation Example:
Result:
Questions? :Questions?
Thank you. :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