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


PHP Tip-a-Day:
PHP Tutorial Convoluted Code - Combining Ternary Operators and Anonymous Functions
May 30, 2012 @ 10:09:40

On the PHP Tip-a-Day site Greg Bulmash shares a bit of "convoluted code" that could potentially cause confusion in the future maintenance of your application - combining ternary with anonymous functions.

Following on yesterday's post about chaining if statements without brackets on a single line, I tried to explore other ways to perform this "test if the variable is set, then do a comparison if it is" logic. I created one of the most convoluted lines of code I've ever written. It's no SQL join that spans 5 whiteboards, but it's pretty unreadable.

His example uses not just one ternary comparison, but nested ones with the anonymous function as the first condition. He points out that, if you're not careful with this method and make both sides anonymous functions, you could be in for a "cannot be converted to string" error on the closure side.

I'm sure there might be a very good reason to put two anonymous functions in a ternary operator, but I can't think of one at the moment. It's a fairly ugly proposition.
0 comments voice your opinion now!
ternary nested anonymous function closure compare


Gonzalo Ayuso's Blog:
Building a small microframework with PHP
August 23, 2011 @ 09:48:27

In investigating microframeworks and some of the offerings out there Gonzalo Ayuso has done a little exploring of his own. He's worked up a basic microframework and shared it in a new post as a sort of academic exercise.

Nowadays microframewors are very popular. Since Blake Mizerany created Sinatra (Ruby), we have a lot of Sinatra clones in PHP world. Probably the most famous (and a really good one) is Silex. But we also have several ones, such as Limonade, GluePHP and Slim. Those frameworks are similar.

He looks at how several of these frameworks handle routing and setup, mostly using the closures/anonymous function callbacks available in PHP 5.3. His simple example framework does some basic URI handling to find the requested module, class and function (action) to call. You can even define the output format from options like json, txt, css, js and jsonp. A sample "controller" is included with a "Hello world" and there's a mention of some other options he's exploring including Twig and Assetic integration.

2 comments voice your opinion now!
microframework exercise routing callback anonymous function


Ryan Gantt's Blog:
Anonymous recursion in PHP
August 11, 2011 @ 10:55:35

In a recent post to his blog Ryan Gantt looks at an interesting way to get around a limitation in PHP dealing with anonymous recursion and closures that throws a Fatal error when called.

Turns out that variables called as functions must be an instance of Closure, an instance of a class which implements __invoke(), or a string representing a named function in the global namespace. In the anonymous function body above, $fibonacci is none of these. It is an undeclared, free variable in the closure created by the anonymous function. At the time when it's called, it hasn't been bound-hence the Notice that you would have gotten if error reporting were set at a high enough threshold - and therefore can't be called as anything, let alone as a function.

He tried using the "use" functionality PHP closures have to bring a variable/object/etc into the scope of the running function, but it still threw an error. As it turns out, the combination of "use"-ing the object and calling it by reference handles things correctly. He takes this method and applies it in two examples - one call in an array_map function and another in an array_reduce.

0 comments voice your opinion now!
anonymous recursion reference invoke closure


Fabian Schmengler's Blog:
Anonymous function calls in PHP
February 25, 2011 @ 09:52:27

Fabian Schmengler has a new post today looking a using anonymous function calls in PHP. He relates to to another popular language that allows for dynamic anonymous functions - Javascript.

Anonymous function calls are a well-known pattern in JavaScript but there are also use cases in PHP where they make sense. Of course PHP 5.3 with its Lambda Functions is required!

He includes several little code snippets showing how the anonymous functions work including the "use" keyword functionality that lets you import variables from outside the function. There's a sneaky pass-by-reference in there, so don't get tripped up.

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


Lorna Mitchell's Blog:
Callbacks in PHP
February 14, 2011 @ 13:41:28

Lorna Mitchell has a new post to her blog today looking at a very handy piece of PHP functionality sprinkled around in different functions - using callbacks to handle complicated processing.

Recently I was working on something and I wanted to call an object method as a callback, but got confused when I realised the method had been caused statically. This was caused by my inability to RTFM and I wondered how I'd come so far without actually coming across the many and varied things you can pass into any place a callback is needed.

Besides the normal callback functions you can put in something like call_user_func, she also mentions something a bit more powerful - passing in an array that contains a pointer to an object and a method inside it. This ability allows you to keep your OOP encapsulation intact without having to make global functions. In PHP 5.3, there's even some of the PHP functions that use call backs that will allow you to use closures/anonymous functions without even having to make a separate function.

0 comments voice your opinion now!
callback calluserfunc function object closure anonymous


Devis Lucato's Blog:
Anonymous objects in PHP - Composition, Mocks, Refactoring
November 23, 2010 @ 13:17:53

In a new post to his blog Devis Lucato points out something he noticed when working with objects and anonymous functions/closures - they're not all as they seem.

Both solutions allow to instantiate an anonymous object with properties. They are used as value objects and have no other purpose than storing values, so no logic can be included and they don't come with methods. They can be used as function parameters instead of arrays, for instance. PHP 5.3.0 introduced anonymous functions and closures, so it is now possible to attach functions to these VOs (*). [...] The first thing to notice is that these properties are not methods but callable functions:

In his example, an anonymous function dynamically appended to an object doesn't have access to a property set on the object just one line before. There's a way around it with call_user_func, but it's not practical. His proposed solution is to create a type of Anonymous class that uses the __call method to catch the methods and translate them into calls to call_user_func_array automatically.

0 comments voice your opinion now!
anonymous objects composition mocking refactoring


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


Jani Hartikainen's Blog:
What is a null object, and when are they useful?
September 14, 2009 @ 12:46:10

In this latest post to his blog Jani Hartikainen looks at creating "null objects" for your applications - a simple tool that lets you replace multiple evaluation checks with a simple object.

How many times have you written code, which checks if a value is null, and then displays something special because of that? Have you written the very same check in more than one place in your code? A null object is an elegant solution to this.

His example shows how to replace a standard User class to grab the user's name with an anonymous user that extends it to return the string "Anonymous User" instead. By creating an intermediate class like this, you can simple call a "getName" and know that there will be some sort of value as the result.

0 comments voice your opinion now!
null object anonymous 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



Community Events





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


community framework interview series symfony language code podcast list laravel zendserver deployment api release introduction conference bugfix tips voicesoftheelephpant threedevsandamaybe

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