Skip to content

callbacks, anonymous or lambda functions - the differences

It just took me a while to really understand the differences between this three. But hopefully (never say never ;-), now i got it. A callback simple describes, that a function awaits another function as parameter. This functions can be an existing one or a "on the fly created on" with the power of create_function.

A lambda function is a function without a function name. The reference is stored in a variable. You can use this variable to handle it over to methods like usort. As everybody i will use a sorting lambda function for showing what i mean.

$lengthOfStringTwo) { $sorter = 1; } elseif ($lengthOfStringOne < $lengthOfStringTwo) { $sorter = -1; } return $sorter; }; $names = array( 'Brian W. Kernighan', 'Dennis Ritchie' ); usort($names, $mySorter); echo xdebug_var_dump($names); ?>

It is also possible to define the $mySorter directly in unsort as second parameter like

usort($names, function($stringOne, $stringTwo { ... });
if you like that.

A closure is a function that surrounds (i do not want to use the word enclose ;-)) the lambda function.

That has the sideeffect that the closure itself conserves their own context. An easy example is a simple logger.

setMessage($message); $myDatabaseInfoLogTable->setTimestamp(mktime()); $status = $myDatabaseInfoLogTable->save(); break; case 'error': $myFileErrorLog = new ErrorLog(); $myFileErrorLog->addMessage(mktime() . "\t" . $message); $status = $myFileErrorLog->save(); break; } return $status; }; } $infoLogger = myClosureLogger('info'); $errorLogger = myClosureLogger('error'); $infoLogger('This is a info log'); $errorLogger('This is an error log'); ?>

The positiv thing about closures is, that the code footprint can be reduced (you even can use closures inside objects - even as static if you do not need to access on the object itself with $this). The negativ thing is, that you can not use interfaces which implies a lose of security.

Translate to de es fr it pt ja

Howto - php array_filter anonymous function

Every day something new :-). This day i finally had time to deal with anonymous functions. Since it is the first time, also after solving the problems, working with anonymous functions is feeling a bit strange. But now i have done it, so it feels better then before where i have never dealt with them.

Here is my example code.

<?php $nl = '<br>' . PHP_EOL;

$array = array( 0 => array( 'level' => 0, 'label' => 'zero', ), 1 => array( 'level' => 1, 'label' => 'one' ),
2 => array( 'level' => 0, 'label' => 'two', ),
3 => array( 'level' => 2, 'label' => 'three', ), ); $levelToFilter = 1;

$array2 = array_filter($array, function ($arrayItem) use ($levelToFilter) { return ($arrayItem['level'] !== $levelToFilter); });

echo 'array::' . $nl; echo xdebugvardump($array); echo 'array2::' . $nl; echo xdebugvardump($array2);

Feel free to play around with it. I guess $levelToFilter is the variable you should change :-).

Translate to de es fr it pt ja

Howto - lambda, anonymous functions (or closures) and aspect oriented programming

What a day, while trying to concentrate on task "digging in into Zend Framework 2" (and this task isn't that easy when manufacturers are setting up a drywall near your desk), i found some exiting pages about "anonymous / lambda functions" and "aspect oriented programming".

Lambda, anonymous function or so called closures are functions that are created "on the fly" are available since php 5.3. Since the upcomming zend framework 2 is using them as often as he can, you have to deal with them - so why not take some minutes and learn about them?

Nice to have, php.net have a howto about them. But i found an even better "how to" on the web. This how to is written by Fabien Potencier and after reading and trying you are feeling much more comfortable by using them.

Last but not least, when i try to understand the event mechanism in the framework, there was the name "Aspect Oriented Programming". Also for this, another webpage can help you to understand what it is all about, try this blog entry.

Translate to de es fr it pt ja