Jump to content

What types of problems are appropriately solved by create_function() ?


jayarsee

Recommended Posts

I searched the forums and the Internet as a whole looking for more information on this, but all I've turned up is a lot of information on /how/ to use create_function(), not /when/ to use it. For instance: http://en.wikipedia.org/wiki/Lambda_calculus

 

Let me reiterate that I understand how it works in general. However, in several years of PHP programming, even through the development of fairly complex, robust systems, I can only think of one case where I recall creating a dynamic function and even here I feel like it was a design mistake.

 

Of course, I use anonymous functions all the time in JavaScript for callback code.

 

It seems to be used from time-to-time to iterate over and apply transformations to sets, but I don't understand the advantage over foreach() for such a task. So, if someone is so-inclined, would you mind listing a general scenario or two where create_function() is the correct and appropriate solution to the problem?

usort($KeywordSuggestions, create_function('$a,$b', 'return strcmp($a["vertical_name"], $b["vertical_name"]);'));

 

Here I could have defined a function called "compare_vertical_name()", but it seems like a waste when it's so simple.  Creating it inline makes sense to me here.  I don't use anonymous functions for anything other than sorting callbacks.  If I used preg_replace_callback() I might also use them as callbacks there.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.