Home  >  Article  >  Backend Development  >  Detailed usage methods in PHP closure function() use()

Detailed usage methods in PHP closure function() use()

步履不停
步履不停Original
2019-06-24 17:15:542744browse

Detailed usage methods in PHP closure function() use()

php's closure (Closure) is also anonymous function. It was introduced in PHP5.3.

The syntax of closure is very simple. The only keyword that needs attention is use. Use means to connect the closure and external variables.

##

Several functions of closure:

1 Reduce the code of foreach loop

1

2

3

[php ] view plain copy

$a =function()use($b) {

##}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

##[php] view plain copy

// A basic shopping cart, including some added products and each quantity of goods.

// One method is used to calculate the total price of all items in the shopping cart. This method uses a closure as a callback function.

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[<code>$product] = $quantity;

}

public function getQuantity($product)

{ ##           

return isset($this->products[$product]) ? $this->products[$product] :                                                                                         

#public function getTotal(

$tax)

{ ##                                                                                               #$callback =                                                                   

,

$product)

use

($tax, &

$total

)                                                                                   #$pricePerItem = constant(CLASS ."::PRICE_"

.

##                          strtoupper($product));                  $total = ($pricePerItem *$quantity) * ($tax

1.0 ); ##             

};

#$this->products,$callback); #return round(

$total, 2);; }

} $my_cart =new Cart; // Add items to shopping cart

$my_cart->add('butter', 1);

$my_cart->add('milk', 3);

$my_cart->add('eggs', 6);

// Print the total price, including 5% Sales tax.

print $my_cart->getTotal(0.05) . "\n";

// The result is 54.29

?>

If we transform the getTotal function here, we must use foreach

2 to reduce the parameters of the function

#If we use the usual method, we will put inner in htmlFunction parameters

1

2

3

4

5

6

7

8

9

[php] view plain copy

function html ($code ,$id="",$class=""){

if ($id !=="")$id =" id = \"$id\"" ;

##$class = ($class !=="")?" class =\"$class\"" :">";

##$open

="&lt ;$code$id$class";

$close

="</$code>" ;

return

function ($inner ="")use ($open,$ close){ ##return

"$open$inner$close";}; }

, so whether it is reading or using the code, it is better to use closures

3 Unlockrecursive functions

1 [php] view plain
2

3

4

5

6

7

8

9

10

copy

##$fib

=function( $n)use(&amp;$fib) {       

if

($n == 0 || $n == 1) return 1;

return

$fib($n - 1) $fib($n - 2);

};

echo

$fib(2) . "\n";// 2

$lie

=$fib;

$fib

=function(){die('error');};//rewrite $fib variable

echo

$lie(5);// error because $fib is referenced by closure

Note that use in the above question uses &. If you do not use & here, an error will occur. n-1) The function cannot be found (the type of fib was not defined previously)

So I want to use closure to release it. When looping functions, you need to use

##1

2

3

4

5

[php] view plain copy

$recursive =function ()use (&amp;$recursive){

// The function is now available as $recursive

}

This form

4 About delayed binding

If you need to delay binding the variables in use , you need to use a reference, otherwise a copy will be made and placed in use when defining

##1Using references or not using references means assigning values ​​when calling or assigning values ​​when declaring The above is the PHP closure I compiled for everyone The detailed usage method in function() use(), I hope it will be helpful to everyone in the future.

2

3

4

5

6

7

8

9

10

11

12

13

[php] view plain

copy

$result

= 0;

$one

=function()

{ var_dump(

$result ); };

$two

=function()use ($result)

{ var_dump(

$result); };

$three

=function() use (&amp;$result)

{ var_dump(

$result ); };

$result

; ##$one

() ; // outputs NULL: $result is not in scope ##$two

();

// outputs int(0): $result was copied $three

();

// outputs int(1)

For more PHP related technical articles, please visit the

PHP Tutorial

column to learn!

The above is the detailed content of Detailed usage methods in PHP closure function() use(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn