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:

  1. calling a variable with the name of a function:
    $foo = "phpinfo";
    $foo();
  2. 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.

Share this content:
  • del.icio.us
  • Google Bookmarks
  • Digg
  • Facebook
  • Technorati
  • MySpace
  • Twitter