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:54 2717browse

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

php's closure (Closure) is alsoanonymous 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 plaincopy

$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 plaincopy

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

classCart

{

constPRICE_BUTTER = 1.00;

constPRICE_MILK = 3.00;

constPRICE_EGGS = 6.95;

protected$products=array();

publicfunctionadd($product,$quantity)

{

##

$this->products[$product] =$quantity;

}

publicfunctiongetQuantity($product)

{##

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

#publicfunctiongetTotal(

$tax)

{###$callback=

,

$product)

use

($tax, &

$total

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

.

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

1.0 );##

};

#$this->products,$callback); #returnround(

$total, 2);;}

}$my_cart=newCart;// 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 thefunction

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

1

2

3

4

5

6

7

8

9

[php] view plaincopy

functionhtml ($code,$id="",$class=""){

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

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

##$open

="< ;$code$id$class";

$close

="";

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(&$fib) {

if

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

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 plaincopy

$recursive=function()use(&$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

##1 Using 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(&$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