Get the App
SLTechnology News&Howtos  ›  Development  › 

Detailed introduction of anonymous functions and precautions of PHP

Shulou Source: shulou.com Published: 2022-06-03 19:46:24 09月16日 Update

This article mainly explains "PHP anonymous functions and precautions in detail," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "PHP anonymous functions and precautions in detail"!

PHP anonymous functions and considerations

Before PHP 5.2: autoload, PDO and MySQLi, type constraints

PHP 5.2: JSON support

PHP 5.3: deprecated features, anonymous functions, new magic methods, namespaces, late static binding, Heredoc and Nowdoc, const, ternary operators, Phar

PHP 5.4: Short Open Tag, Array Abbreviations, Traits, Built-in Web Server, Detail Modifications

PHP5.5: yield, list() for foreach, detail modification

PHP 5.6: Constant enhancements, variable function parameters, namespace enhancements

Now basically all use PHP 5.3 after the version, but I feel that a common phenomenon is a lot of new features, after such a long time, has not been fully popular, rarely used in the project.

Take a look at PHP anonymous functions:

'test' => function(){ return 'test'},

PHP anonymous function definition is very simple, is to assign a variable, but this value is a function.

The above is the configuration using the Yii framework configuration components file, plus a test configuration.

What are PHP anonymous functions?

See official explanation:

Anonymous functions, also known as closures, allow the temporary creation of a function without a specified name. The value most often used as a callback parameter. Of course, there are other applications.

Anonymous function example

Closure functions can also be used as values of variables. PHP automatically converts this expression into an object instance of the built-in Closure class. The syntax for assigning a closure object to a variable is the same as for ordinary variable assignments, with a semicolon added at the end:

Anonymous function variable assignment example

Closures can inherit variables from parent scopes. Any such variables should be passed in using the use language construct.

Inherit variables from parent scope

Tags: Functions variables items notes closures actions contents parameters objects that is methods times examples space details learning configuration deeper two examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Docker MariaDB Xiaomi Shulou Tech Info