PHP Anonymous functions and considerations

Source: Internet
Author: User


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.

Try printing on another template page:

Test? >//test
Ok.

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 ();
&NBSP
//inherited variable ' s value is from/' the ' function
/* defined, not when called
$message = ' wor LD '
Echo $example ();
&NBSP
//Reset message
$message = ' Hello '
 
//Inherit by-reference
$example = function () ; $message) {
    var_dump ($message);
};
Echo $example ();
&NBSP
//The changed value in the parent scope
/are 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");
?

The output of the above routine is similar to the following:


notice:undefined Variable:message in/example.php on line 6
Null
String (5) "Hello"
String (5) "Hello"
String (5) "Hello"
String (5) "World"
String (one) "Hello World"


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 ();
It is printed out to be a AA;

Look at the following example:

Echo $callback ();
$callback =function () {
return "AA";
};
This is an error! When you report the error:

notice:undefined Variable:callback in D:\php\www\zf2\public\04.php on line 9
Fatal error:function Name must is a string in D:\php\www\zf2\public\04.php on line 9

$callback is not declared,

But using PHP's own declared function will not be 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!!!!!

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.