PHP anonymous functions and considerations

Source: Internet
Author: User
What do we need to pay attention to when writing PHP anonymous functions? this article mainly introduces the relevant information about PHP anonymous functions and precautions. the anonymous function is introduced in PHP5.3, php5.3 not only introduces anonymous functions, but also introduces more and more new features. let's take a look at PHP anonymous functions and considerations. For more information, see.

PHP anonymous functions and precautions

Before PHP5.2: autoload, PDO and MySQLi, type constraints
PHP5.2: JSON support
PHP5.3: deprecated functions, anonymous functions, new magic methods, namespaces, static binding later, Heredoc and Nowdoc, const, ternary operators, Phar
PHP5.4: Short Open Tag, abbreviated array format, Traits, built-in Web server, detailed modification
PHP5.5: yield, list () for foreach, details modification
PHP5.6: constant enhancement, variable function parameters, and namespace enhancement

Currently, PHP and later versions are basically used. However, a common phenomenon is many new features. after such a long period of time, it has not been fully popularized and is rarely used in projects.

Look at the PHP anonymous functions:

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

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

The preceding section uses the Yii Framework to configure the components file and adds a test configuration.

What is a PHP anonymous function?

See the official explanation:

An Anonymous function (Anonymous functions), also called a closure function (closures), allows you to temporarily create a function without a specified name. The value that is most often used as the callback function parameter. Of course, there are other applications.

Anonymous function example:

 

Closure functions can also be used as variable values. PHP automatically converts this expression to an object instance with built-in Closure class. The method for assigning a closure object to a variable is the same as the syntax for assigning values to common variables. add a semicolon to the end:

Example of anonymous function variable assignment:

 

The closure can inherit variables from the parent scope. Any such variables should be passed in with the use language structure.

Inherit variables from parent scope

 

Considerations for anonymous functions in php

After php5.3, When php is used as an anonymous function, an error occurs today when it is used as an anonymous function. You cannot declare or use it like the php function. For more information, see the code.

$callback=function(){  return "aa"; }; echo $callback();

Printed as aa;

See the following example:

echo $callback(); $callback=function(){  return "aa"; };

An error is reported! $ Callback is not declared, but no error is reported for functions declared by php!

function callback(){  return "aa"; } echo callback(); //aa   echo callback(); //aa function callback(){  return "aa"; }

Both of them are printed out as aa;

When using an anonymous function, the anonymous function must be declared as a variable in advance. this is also true in js!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.