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