Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of anonymous functions and precautions of PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report