Recently, when learning Workerman, more frequent contact with the callback function, often in use because of the way the worker is used, the two different ways to invoke the external worker variables, Just tidy up the PHP closure. Gets the difference between the external variable and the global keyword declaration variable.
Closed Package
Closures are a common concept that we can often use with callback functions to make the code more concise and readable.
Closures can be copied in such a way that the function uses variables from the parent scope. Such as:
$global = ‘hello‘;$bbb = function()use($global){ echo $global."\n";};$bbb();//输出 ‘hello‘
Global keyword Declaration variable
Declaring a variable through global also allows the function body to invoke variables outside of the function, but unlike use, the Globle keyword causes a reference to be created with the same name as the external variable , and changes to the variable within the function will also affect the extraterritorial variables.
$global = ‘hello‘;$fun = function(){ global $global; $global =‘ world‘; echo $global."\n";};$fun();// 输出 ‘world‘
This simply creates a reference to the same name, and does not change the scope of the original external variable $global, which means that the call in another function still needs to be declared or used closures
$global = ‘hello‘;$fun = function(){ global $global; $global =‘ world‘; echo ‘a:‘.$global."\n";};$ccc = function(){ echo ‘b:‘.$global;};$fun()$ccc()
/*输出a: worldNotice: Undefined variable: global in xxxxxxx on line xx*/
Change the code a little bit, which makes it easier to compare the two ways of accessing external variables, such as the closure and global keyword declaration variables.
<?php$global = ‘hello‘;$fun = function(){ global $global; $global =‘world‘; echo ‘a:‘.$global."\n";};$bbb = function()use($global){ $global = ‘china‘; echo ‘c:‘.$global."\n";};$fun();echo ‘b:‘.$global."\n";$bbb();echo ‘d:‘.$global;
Here B and D two outputs can be seen, global changes the value of the external variable, and the closure method does not.
Output:
a: worldb: worldc:chinad: world
Finally, a more classic example of using anonymous functions, closures and callback functions in an official document:
class cart{Const Price_butter = 1.00; Const PRICE_MILK = 3.00; Const PRICE_EGGS = 6.95; Protected $products = Array (); Public function Add ($product, $quantity) {$this->products[$product] = $quantity; } Public Function getquantity ($product) {return isset ($this->products[$product])? $this->products[$PR Oduct]: FALSE; The Public Function gettotal ($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); Return round ($total, 2); }} $my _cart = new cart, $my _cart->add (' Butter ', 1), $my _cart->add (' Milk ', 3); $my _cart->add (' eggs ', 6);p rint $my _cart->gettotal (0.05). "\ n";
PHP closures Get the difference between an external variable and the Global keyword declaration variable