PHP anonymous functions (closures)

Source: Internet
Author: User
Anonymous Functions(Anonymous functions), also called closure function(closures), which 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.

Combine array_walk and anonymous functions to implement a billing function

The array_walk () function applies a callback function to each element in the array. Returns TRUE if successful, otherwise FALSE.

1
 _productsort = Array (' Iphone6 ', ' xiaomi4 ', ' Honerx '); $this->_products = Array ();}15-Public funct             Ion Addproduct ($product, $sum) {if (In_array ($product, $this->_productsort) && is_int ($sum)) {18 $this->_products[$product] = $sum,}20}21 public Function GetCount ($tax) {$coun t = 0;24 $callback = function ($sum, $product) use ($tax, & $count) {$price = constant (__clas S__. ':: '. Strtoupper ($product). ' _price ') $curPrice = $price * $sum * $tax; $count + = $curPrice;};30 arra Y_walk ($this->_products, $callback), return $count,}34}35 $oanyMouse = new anymousetest;37 $oan Ymouse->addproduct (' Iphone6 ', 1), $oanyMouse->addproduct (' Honerx ', 2), Echo $oanyMouse->getcount (1);

Where constant is used to return the value of a string form constant

  • 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.