PHP closure function Detailed _php tips

Source: Internet
Author: User
Tags anonymous closure constant function definition

Anonymous functions are also called closure functions (closures allows you to create a function that has not been specified, most often as a value for a callback function parameter.)

The closure function does not have a function name, and the defined variable is treated as a function directly when the function () is passed into the variable

  $CL = function ($name) {return
    sprintf (' Hello%s ', name);
  }
  echo $cli (' fuck ')

Called directly by a variable name defined as an anonymous function.

echo Preg_replace_callback (' ~-([A-z]) ~ ', function ($match) {return
  strtoupper ($match [1]);
}, ' Hello-world ' ;`

Using use

$message = ' Hello ';
$example = function () use ($message) {
  var_dump ($message);
Echo $example ();
Output Hello
$message = ' world ';
Output Hello because the value of the inherited variable is the time of the function definition rather than when the function is called
echo $example ();
Reset to Hello
$message = ' Hello ';
Reference
$example = function () use (& $message) {
 var_dump ($message);
Echo $example ();
Output Hello
$message = ' world ';
Echo $example ();
the World//closure function is also used for normal pass values
$message = ' Hello ';
$example = function ($data) use ($message) {return
  "{$data},{$message}";

echo $example (' World ');

Example

Class cart{//Defines constants in the class with the Const keyword, rather than the usual define () function.
  Const Price_butter = 1.00;
  Const PRICE_MILK = 3.00;

  Const PRICE_EGGS = 6.95;
  protected $products = [];
  Public function Add ($product, $quantity) {$this->products[$product] = $quantity; The Public Function getquantity ($product) {//Does the return isset ($this->products[$product]) be defined? $this->products[$
  Product]:false;
    The Public Function gettotal ($tax) {$total = 0.0; $callback = function ($quantity, $product) use ($tax, & $total) {//constant Returns the value of a constant//__class__ the class name $PR

      Ice = constant (__class__. "::P rice_". Strtoupper ($product));
    $total + + ($price * $quantity) * ($tax +1.0);
    }; The Array_walk () function applies a user-defined function to each element in the array.
    In the function, the key name and the key value of the array are parameter Array_walk ($this->products, $callback);

  Callback anonymous function return round ($total, 2);
}} $my _cart = new cart ();
$my _cart->add (' Butter ', 1);
$my _cart->add (' Milk ', 3);


$my _cart->add (' eggs ', 6);
 Print ($my _cart->gettotal (0.05));

The above is about the PHP closure function of the relevant content, I hope to help you learn.

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.