PHP anonymous functions and notes detailed introduction _php tips

Source: Internet
Author: User
Tags anonymous closure

PHP Anonymous functions and considerations

PHP5.2 before: AutoLoad, PDO and mysqli, type constraints
Php5.2:json Support
PHP5.3: Deprecated functions, anonymous functions, New magic methods, namespaces, late-static bindings, Heredoc and Nowdoc, const, ternary operators, Phar
Php5.4:short Open Tag, array shorthand form, Traits, built-in WEB server, details modification
Php5.5:yield, List () for foreach, detail modification
PHP5.6: Constant enhancements, variable function parameters, namespace enhancements

Now basically use PHP5.3 later version, but the feeling common one phenomenon is many new features, after such a long time, not fully popularized, in the project rarely used.

Look at the PHP anonymous function:

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

The simple definition of a PHP anonymous function is to assign a value to a variable, except that the value is a function.

The above is to configure the components file with the YII framework and add a test configuration.

What is a php anonymous function?

See Official explanation:

anonymous functions (Anonymous functions), also called closure functions (closures), allow temporary creation of a function without a specified name. The value most often used as a callback function (callback) parameter. Of course, there are other applications as well.

anonymous function Example

<?php
Echo preg_replace_callback (' ~-([A-z]) ~ ', function ($match) {return
  strtoupper ($match [1]);
}, ' Hello-world ');
Output HelloWorld
?>

A closure function can also be used as a value for a variable. PHP automatically converts this expression into an object instance of the built-in class Closure. Assigning a closure object to a variable is the same as the syntax for assigning values to a normal variable, and finally a semicolon is added:

anonymous function Variable Assignment example

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

Closures can inherit variables from the parent scope. Any such variable should be passed in using the use language structure.

Inherit a variable from a parent scope

<?php
$message = ' Hello '
//No ' use '
$example = function () {
  var_dump ($message);
Echo $example ();
Inheritance $message
$example = function () use ($message) {
  var_dump ($message);
Echo $example ();
Inherited variable ' s value
is from when the function/are defined, not when called
$message = ' World ' echo $e Xample ();
Reset message
$message = "Hello"
//Inherit by-reference
$example = function () use (& $message) {
   var_dump ($message);
Echo $example ();
The changed value in the parent scope
//are reflected inside the function call
$message = ' World ' echo $exampl E ();
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, PHP to join the use of anonymous functions, today in the use of anonymity when there are errors, you can not think of PHP functions such as declaration and use, see the code in detail

$callback =function () {return 
 "AA"; 
}; 
Echo $callback (); 

Print out is a AA;

Look at the following example:

Echo $callback (); 
$callback =function () {return 
 "AA"; 
}; 

This is an error! $callback is not declared, but the functions declared using PHP do not give an error!

function callback () {return 
 "AA"; 
} 
Echo Callback (); AA 
 
Echo Callback ();//aa 
function callback () {return 
 "AA"; 
} 

Both of these are printed out AA;

In the use of anonymous functions, anonymous functions as variables, must be declared in advance, JS is also such a!!!!!

Thank you for reading, I hope to help you, thank you for your support for this site!

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.