The anonymous function (Anonymous functions), also called the closure function (closures), allows temporary creation of a function without a specified name. The value most often used as the callback function (callback) parameter. Of course, there are other applications as well.
Example #1 Anonymous Function example
<?php
Echo preg_replace_callback ( ' ~-([A-z]) ~ ' , function ( $match ) {
return strtoupper ( $match [1}, ' Hello-world ' // output helloworld
?>
The closure function can also be used as the value of 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 a value to a normal variable, with a semicolon at the end:
Example #2 anonymous function variable Assignment example
= function ( $name )
{
printf ( "hello %s\r\n" , $name );
};
$greet ();
$greet PHP" Span style= "color: #007700;" >);
?>
Closures can inherit variables from the parent scope. Any such variables should be passed in using the use language structure.
Example #3 inherit variables from the 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) {
Var_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");
?>
The output of the above routines is similar to the following:
notice:undefined variable:message in/example.php on line 6NULLstring (5) "Hello" string (5) "Hello" string (5) "Hello" Strin G (5) "World" string (one) "Hello World"
These variables must be declared on the head of a function or class. Inheriting variables from the parent scope is different from using global variables. Global variables exist in a global scope, regardless of which function is currently executing. The parent scope of the closure is the function that defines the closure (not necessarily the function that calls it). Examples are as follows:
Example #4 Closures and scopes
<?php
A basic shopping cart, including some already added items and the quantity of each item.
There is one way to calculate the total price of all the items in the shopping cart, and this method makes
A closure is used as the callback function.
ClassCart
{
ConstPrice_butter=1.00;
ConstPrice_milk=3.00;
ConstPrice_eggs=6.95;
Protected$products= Array ();
Public functionAdd($product,$quantity)
{
$this-Products[$product] =$quantity;
}
Public functionGetquantity($product)
{
Return Isset ($this-Products[$product]) ?$this-Products[$product] :
FALSE;
}
Public functionGettotal($tax)
{
$total=0.00;
$callback=
function ($quantity,$product) Use ($tax, &$total)
{
$pricePerItem=constant(__class__."::P Rice_".
Strtoupper($product));
$total+= ($pricePerItem*$quantity) * ($tax+1.0);
};
Array_walk($this-Products,$callback);
ReturnRound($total,2);;
}
}
$my _cart= newCart;
Add items to your shopping cart
$my _cart-Add(' Butter ',1);
$my _cart-Add(' Milk ',3);
$my _cart->add ( eggs ' , 6);
// the total price, which has a sales tax of 5% .
print $my _cart- >gettotal0.05< Span style= "color: #007700;" >) . // The final result is 54.29
?>
Anonymous functions are now implemented through the Closure class.
Update log
| version |
Description |
| 5.4.0 |
$thiscan be used for anonymous functions. |
| 5.3.0 |
You can use anonymous functions. |
PHP Closures--Anonymous functions