Anonymous functions in PHP
12 giugno 2008, 9:09 Open Source , PHP , Tips & Tricks June 12, 2008, 9:09
PHP is a good programming language, if we judge with enough empirical parameters of diffusion and the amount of function that integrates.
Often proves to be surprisingly flexible programming scenarios for addressing much more complex than simply embedding of scripts into a webpage.
During a recent exchange of views with a mathematician fond of Open Source has presented the problem of creating anonymous functions, and - here's the rub - not inside a PHP class. Skipping the theoretical claptrap that I had to sip on lambda calculus and anonymous functions, and I will only present here a passage of the definition given by WikiPedia . I quote:
The lambda calculus is a rewriting system defined formally by the mathematician Alonzo Church . It was developed to analyze formally the definitions of functions , their applications and is also an interesting tool for studying phenomena of recursion . As the rewriting system, it gives a description of the terms well formed, which are the sequences of symbols recognized by the system and able to be rewritten from it. The lambda calculus, in fact, defines a set of rewriting rules that determine precisely how these terms can be rewritten. In this way, the process of rewriting becomes a real calculation .
And yet (available in English only):
Anonymous functions can be used to Contain functionality That need not be named and Possibly for short-term use. Some notable examples include closures and currying. [...]
Closures are functions Evaluated in an environment containing bound variables. The following example binds the variable "threshold" in an anonymous function That Compares the input to the threshold. [...]
Currying is a transforming function from multiple inputs to fewer inputs (in this case integer division).
Although not officially exist in PHP a real method to handle anonymous functions, you can easily get something similar in at least two ways:
-
calling a variable with the name of a function:
$foo = "phpinfo";
$foo(); -
initializing a variable with the `` create_function:
$sum = create_function('$a, $b','return $a + $b;');
echo $sum(1,2);
In the first case we are only creating a link to the function. In the second case, we assign the function to the variable just as we create it. Seeing is believing: it is an unusual and somewhat fun of PHP, but strongly discourage anyone to adopt a similar solution in production environments where you do not want to lose sight of practicality and ease of reading the code.















June 12th, 2008 at 9:56
Hello Ivan, I am helping a friend with her honors degree in communication sciences where one of the chapters is the incompleteness theorem of Godel, who I understand that central to the lambda calculus. I have to make something that is an example of Godel's theorem, you can help me? Qualsiai programming language goes well, PHP, which is simple enough to explain to the supervisor. Thanks
June 12th, 2008 at 15:31
Hello, you have not signed and your email address will not understand much about leaving your name ...
It is strange that you do me this question, because one of my obsessions of the past by researchers in the disciplines derived from semiotics are precisely the theorems (to be precise: they are 2) Kurt Gödel incompleteness.
I'm busy but not too long ago to be able to help me now ask, are now almost 10 years now. But at least I can help you find an idea that it represents the least popular version (I think it's more than enough for a thesis) or that contained in the voluminous and famous Hofstadter's book "Gödel, Escher, Bach: an Eternal Golden Braid ". You read it?
Why have you added a comment and have not written directly (perhaps from the form "Contact")?
Aspect of your news