[Question] PHP Anonymous functions

GarettM

Posting Freak
Aug 5, 2010
833
136
When would it be a good idea to use anonymous functions.

I like doing this for simple stuff like:
PHP:
<?php
$info = function() {
   Return Array(
       'Example' => 'I am anonymous function'
   );
};
echo $info()['Example'];
I was also wondering is this a good programming habit?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Looks too complicated and the syntax is ugly in my opinion.

What's wrong with just:
PHP:
<?php echo 'I am <i>not</i> an anonymous function.'; ?>
 

Adil

DevBest CEO
May 28, 2011
1,278
716
When would it be a good idea to use anonymous functions.

I like doing this for simple stuff like:
PHP:
<?php
$info = function() {
   Return Array(
       'Example' => 'I am anonymous function'
   );
};
echo $info()['Example'];
I was also wondering is this a good programming habit?
Anonymous functions are useful when you only need 1 function (e.g a specific callback).
Something like:
Code:
void handle_data(char *data, void *func) {
    func(data);
}
 
Last edited:

Users who are viewing this thread

Top