This article mainly introduces the PHP anonymous function and notice details of the relevant information, the anonymous function is PHP5.3 introduced, php5.3 not only introduced anonymous function and more and more new features, let's look at the PHP anonymous function and notes in detail, A friend you need can refer to the following
PHP Anonymous functions and considerations
PHP5.2 before: AutoLoad, PDO, and mysqli, type constraints
Php5.2:json Support
PHP5.3: Deprecated features, anonymous functions, New magic methods, namespaces, late static bindings, Heredoc and Nowdoc, const, ternary operators, Phar
Php5.4:short Open Tag, array shorthand form, Traits, built-in Web server, detail modified
Php5.5:yield, List () for foreach, detail modification
PHP5.6: constant enhancement, variable function parameters, namespace enhancement
Now basically use PHP5.3 later version, but feel common a phenomenon is a lot of new features, after so long time, not fully popularized, rarely used in the project.
Look at the PHP anonymous function:
' Test ' = function () { return ' test '},
The definition of an anonymous PHP function is simply to assign a value to a variable, except that the value is a function.
The above is configured with the YII framework to configure the components file, plus a test configuration.
What is the PHP anonymous function?
Look at the official explanation:
The anonymous function (Anonymous functions), also called the closure function (closures), allows temporary creation of a function without a specified name. The value most often used as the callback function (callback) parameter. Of course, there are other applications as well.
anonymous function Example
<?phpecho preg_replace_callback (' ~-([A-z]) ~ ', function ($match) { return Strtoupper ($match [1]);}, ' Hello-world ');//Output helloworld?>
The closure function can also be used as the value of a variable. PHP automatically converts this expression into an object instance of the built-in class Closure. Assigning a closure object to a variable is the same as the syntax for assigning a value to a normal variable, with a semicolon at the end:
Example of an anonymous function variable assignment
<?php$greet = function ($name) { printf ("Hello%s\r\n", $name);}; $greet (' World '); $greet (' PHP '); >
Closures can inherit variables from the parent scope. Any such variables should be passed in using the use language structure.
Inherit variables from parent scope
<?php$message = ' Hello '//No ' use ' $example = function () { var_dump ($message);}; Echo $example ();//inherit $message $example = function () use ($message) { var_dump ($message);}; Echo $example ();//inherited variable ' s value was from when the function//was defined, not when called$message = ' World ' Ech o $example ();//Reset message$message = ' Hello '//Inherit by-reference$example = function () use (& $message) { var_ Dump ($message);}; Echo $example ();//The changed value in the parent scope//was reflected inside the function call$message = ' World ' echo $ex Ample ();//Closures can also accept regular arguments$example = function ($arg) use ($message) { var_dump ($arg. ' ' . $message);}; $example ("Hello"); >
Considerations for anonymous functions in PHP
After php5.3, PHP joins the use of anonymous functions, today in the use of anonymous errors, can not be declared and used as PHP functions, detailed look at the code
$callback =function () { return "AA";}; echo $callback ();
Print out is AA;
Look at the following example:
Echo $callback (); $callback =function () { return "AA";};
This is an error! $callback is not declared, but using PHP's own declared function will not be an error!
function callback () { return "AA";} Echo callback ();//aa Echo Callback ();//aa function callback () { return " AA "; }
Both of these are printed out AA;
When using anonymous functions, the anonymous function as a variable, must be declared in advance, JS is the same!!!!!
The above is the whole content of this article, I hope that everyone's study has helped.