Home>Article>Backend Development> Why does php need anonymous functions?
Anonymous functions, also called closures, allow the temporary creation of a function without a specified name. The value most commonly used as a callback function argument. Of course, there are other applications as well.
Anonymous functions are currently implemented through the Closure class.
Anonymous function example(Recommended learning:PHP programming from entry to proficiency)
Closure functions can also be used as the value of variables . PHP will automatically convert this expression into an object instance of the built-in class Closure. The method of assigning a closure object to a variable is the same as the syntax of ordinary variable assignment, and a semicolon must be added at the end:
Anonymous function variable assignment example
Closures can inherit variables from the parent scope. Any such variables should be passed in using the use language construct. Starting from PHP 7.1, such variables cannot be passed in: superglobals, $this or have the same name as parameters.
The above is the detailed content of Why does php need anonymous functions?. For more information, please follow other related articles on the PHP Chinese website!