News Feed
Sections




News Archive
feed this:

Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Andrew Podner:
Closures, Lambdas, and Anonymous Functions
February 21, 2013 @ 13:02:13

Andrew Podner has posted an introduction to a concept in PHP that may not be easy to immediately grasp for someone relatively new to the language - the idea of lambdas and closures.

Beginning with PHP 5.3, the anonymous function, became available to developers as a means of improving flexibility and increasing the number of tools at our fingertips to build powerful PHP applications. What I have been seeing a lot of as I read through documentation is more frequent use of terms like lambda, closure, or anonymous function, and sometime the terms are often used interchangeably, so let's take a deeper look into what all of these things are and what differences, if any, there are between them.

The post looks at lambdas first, defining them as "functions without names" that can be assigned to objects or called inline when needed. Next is closures, and it's noted that, in PHP, there's really not much difference. They both also allow for use of the "use" statement to import values into the function's local scope. He mentions the "$this" update that came in PHP 5.4 and includes some arguments against their use too.

0 comments voice your opinion now!
closure lambda anonymous function introduction


Shameer's Blog:
PHP 5.3 Practical look into Lambda functions and closures
December 30, 2010 @ 11:11:26

For those out there still coming to grips with some of what PHP 5.3 has to offer, I'd suggest checking out this new article from Shameer about using lambdas and closures along with some examples putting them into action.

PHP 5.3 packaged the power of functional programming by adding support for lambda functions and closures. You will be familiar with them in javascript. In this article we will have a look into these features and its usages.

He starts with explanations of what they are with simple code examples showing their use - a lambda assigned to a variable and closures with the "use" keyword. He shows how they can be used in application prototyping, making handy callback functions and includes a practical example of finding the factorial of a number using a lambda.

2 comments voice your opinion now!
lambda closure tutorial practical example


SaniSoft Blog:
Lambda functions and Closures in PHP 5.3c
October 12, 2010 @ 10:59:38

On the SaniSoft blog there's a recent post looking at one of the more recent and more powerful features to be included in PHP (as of 5.3) - lambdas and closures.

PHP 5.3 has introduced a whole new range of cool features like Namespaces, late static binding, new error levels, to name a few. But the functionality that I found most handy for day to day programming is the Lambda functions and Closures that found their way in PHP through version 5.3

The introduce the concept of the lambda/closure functions and how they can be used instead of several of the usual callback functions. In their example, they give an array of data (associative inside of a numeric key) and an array_filter call with a closure callback to check the value of the "score" value in each. Their closure example assigns the function to a variable to return the score information from the same dataset.

0 comments voice your opinion now!
closure lambda tutorial function


Sameer Borate's Blog:
Anonymous functions in PHP
June 07, 2010 @ 12:42:33

On his blog today Sameer Borate has a new post talking about anonymous functions (closures, lambdas) in PHP and includes plenty of examples of how to use them.

Anonymous functions are common in various modern languages, Ruby and Javascript being the popular one. But until version 5.3 PHP lacked true anonymous functions. Although newbie programmers are hard-pressed to find a suitable application for anonymous functions, they are indispensable if you do a lot of OOP, and can provide some elegant solutions to some particular problems.

He starts with a look at variable functions both in procedural code an object-oriented then moves to the anonymous/lambda function examples (with some nexting involved) and a few uses for closures.

0 comments voice your opinion now!
anonymous function closure lambda tutorial


Eli White's Blog:
An intriguing use of lambda functions
March 11, 2010 @ 08:48:16

In a new post to his blog today Eli White takes a look at an interesting use of lambda functions he's figured out for a his development at work.

I found a very specific use out of the blue of Lambda functions that I have now used and I see as a great use-case. Which is specifically passing functions/logic from your Controller to your View. In the case of Goodsie, I'm using PHP for my templating language and as usual I'm trying to remove as much logic from my View as possible, while still allowing the view to be malleable.

His method centered around a pagination subview that he wanted to make flexible enough to work with both the standard page view and with an ajax request. Lambda functions came to the rescue by dynamically creating a function based on the request need.

0 comments voice your opinion now!
lambda function pagination subview tutorial


Recess Blog:
Functional PHP 5.3 Part I - What are Anonymous Functions and Closures?
August 19, 2009 @ 11:56:50

Those still trying to get a handle on anonymous functions, lambdas and closures in the recently released 5.3 version of PHP might want to take a look at this new tutorial from the Recess blog. It's the first part of their "Functional PHP 5.3" series.

One of the most exciting features of PHP 5.3 is the first-class support for anonymous functions. You may have heard them referred to as closures or lambdas as well. There's a lot of meaning behind these terms so let's straighten it all out.

They explain the differences between closures and lambda functions (hint: not much) and give code examples for both them and closures.

0 comments voice your opinion now!
anonymous function closure lambda


Dagfinn Reiersol's Blog:
Real programming with PHP 5.3 (part 4) A more realistic example
June 05, 2009 @ 07:57:21

On the PHP in Action blog Dagfinn Reiersol continues his look at real programming examples using the upcoming PHP 5.3 release. In this new tutorial he focuses on making a classed based on the Range design pattern to paginate results.

A Range object is defined by just two values (three in my variation), so it might seem like too much to have an object just to keep these values, but as you can see from the example, a Range class can have behaviors to change and compare ranges.

He gives two code snippets - the first defines most of the methods of the class in the constructor (using the lambda functions) and the second uses a __call to catch the calls to the same methods but defined normally as private (making them inaccessible to the outside world).

1 comment voice your opinion now!
lambda designpattern range tutorial


PHPClasses.org:
Upcoming PHP 5.3 features and beyond
April 28, 2009 @ 07:56:34

This new post to the PHPClasses.org blog take a look at some of the new features that will be included in the PHP 5.3 release (coming soon to a web server near you!) by way of an interview with Lukas Smith.

his article presents an interview with core PHP developer Lukas Kahwe Smith that has pushed many of the new features of PHP 5.3 as release manager. Lukas talks about PHP 5.3 new features such as lambda functions, closures and PHAR support. He also explains what are traits, which for now it is a feature that was left out of PHP 5.3. He also talks about future PHP 5.x and PHP 6, as well how anybody can help in the development of PHP to make it come out faster.

They talk about the PHP.net wiki, briefly touch on the PHP 5.3 feature updates, performance/memory usage in the new version, Lambda functions, closures and PHAR and what's to come with PHP 6.

0 comments voice your opinion now!
lukassmith php5 upcoming php6 interview phar closures lambda memory usage


Fabien Potencier's Blog:
On PHP 5.3, Lambda Functions, and Closures
April 17, 2009 @ 10:29:43

In this new post to his blog Fabien Potencier looks at two of the much-hyped features of the upcoming PHP 5.x series release (5.3) - closures and lambda functions.

I won't talk too much about what lambda functions or closures are, as you can find many good blog posts describing them in great details. To sum up, a lambda function is an anonymous PHP function that can be stored in a variable and passed as an argument to other functions or methods. A closure is a lambda function that is aware of its surrounding context.

He includes several examples including how they would work with a few of the array functions, an implementation of the Y-combinator method (as written by Stanislav Malyshev) and how they can be used to create dependency injection functionality.

2 comments voice your opinion now!
lambda function closure php5 array ycombinator dependency injection container


Dagfinn Reiersol's Blog:
Real programming with PHP 5.3 (part 3) Links
April 13, 2009 @ 07:58:41

On his blog today Dagfinn Reiersol has posted the next part of his "real programming with PHP 5.3" series looking at links.

After the previous post in this series, additional independent implementations of the idea of JavaScript-style classes have turned up. So I'm going to list them and comment briefly on the differences. I hope this will be helpful to anyone who actually wants to use this in practice and needs to decide on the details of the implementation.

The previous article looked at Javascript-style classes that can be build with the new closures/lambda function support that PHP 5.3 will include. A few of the other examples of this method he mentions include this look from Ionut Stan's forum post and Andrea Giammarchi's blog post on making a Javascript object-like class.

0 comments voice your opinion now!
php5 real programming series javascript object class closure lambda



Community Events





Don't see your event here?
Let us know!


install composer series security podcast framework package library symfony voicesoftheelephpant interview update language community tool release introduction opinion laravel version

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework