PHP anonymous functions and precautions

Source: Internet
Author: User
This article mainly introduces PHP anonymous functions and precautions in detail. anonymous functions are introduced in PHP5.3, if you want to learn about anonymous functions, refer to php5.3, which not only introduces anonymous functions, but also introduces more and more new features. Next, let's take a look at the PHP anonymous functions and precautions. the specific content is as follows:

Before PHP5.2: Autoload, PDO, and MySQLi, type constraints
PHP5.2:JSON support
PHP5.3:Abandon function, anonymous function, new magic method, namespace, static binding later, Heredoc and Nowdoc, const, ternary operator, Phar
PHP5.4:Short Open Tag, abbreviated form of array, Traits, built-in Web server, detailed modification
PHP5.5:Yield, list () for foreach, detailed 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:

Anonymous functions ),It is also called a closure function (closures). a function with no specified name can be created temporarily. The value that is most often used as the callback function parameter. Of course, there are other applications.

Anonymous function example

<? Phpecho preg_replace_callback ('~ -([A-z]) ~ ', Function ($ match) {return strtoupper ($ match [1]) ;}, 'Hello-world'); // output helloWorld?>

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:

Anonymous function variable assignment example

<?php$greet = function($name){ printf("Hello %s\r\n", $name);};$greet('World');$greet('PHP');?>

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

<? 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 is from when the function // is defined, not when called $ message = 'world' echo $ example (); // Reset message $ message = 'hello' // Inherit by-reference $ example = function () use (& $ message) {v Ar_dump ($ message) ;}; echo $ example (); // The changed value in the parent scope // is reflected inside the function call $ message = 'world' echo $ example (); // 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, 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 !!!!!

The above are the anonymous PHP functions and precautions for everyone, and I hope they will help you with your learning.

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.