亚洲欧美日韩综合系列在线_91精品人妻一区二区_欧美大肥婆一级特大AA片_九色91视频免费观看_亚洲综合国产精品_av中文字幕在线不卡_久久精品色综合网_看黄色视频的软件_无卡无码高清中文字幕码2024_亚洲欧美日韩天堂网

PHP閉包定義與使用簡單示例php技巧

來源:阿斌啊 發(fā)布時(shí)間:2018-12-05 15:28:30 閱讀量:1174

這篇文章主要介紹了PHP閉包定義與使用,結(jié)合簡單實(shí)例形式分析了php閉包的簡單定義、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了PHP閉包定義與使用。分享給大家供大家參考,具體如下:


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

<?php

function getClosure($i)

{

  $i = $i.'-'.date('H:i:s');

  return function ($param) use ($i) {

    echo "--- param: $param ---\n";

    echo "--- i: $i ---\n";

  };

}

$c = getClosure(123);

$i = 456;

$c('test');

sleep(3);

$c2 = getClosure(123);

$c2('test');

$c('test');

/*

output:

--- param: test ---

--- i: 123-21:36:52 ---

--- param: test ---

--- i: 123-21:36:55 ---

--- param: test ---

--- i: 123-21:36:52 ---

*/


再來一個(gè)實(shí)例


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

$message = 'hello';

$example = function() use ($message){

 var_dump($message);

};

echo $example();

//輸出hello

$message = 'world';

//輸出hello 因?yàn)槔^承變量的值的時(shí)候是函數(shù)定義的時(shí)候而不是 函數(shù)被調(diào)用的時(shí)候

echo $example();

//重置為hello

$message = 'hello';

//此處傳引用

$example = function() use(&$message){

 var_dump($message);

};

echo $example();

//輸出hello

$message = 'world';

echo $example();

//此處輸出world

//閉包函數(shù)也用于正常的傳值

$message = 'hello';

$example = function ($data) use ($message){

 return "{$data},{$message}";

};

echo $example('world');

//此處輸出world,hello



您可能感興趣的文章:

php微信公眾號(hào)開發(fā)之現(xiàn)金紅包php實(shí)例

PHP代碼重構(gòu)方法漫談_php技巧

PHP實(shí)現(xiàn)負(fù)載均衡下的session共用功能php技巧


標(biāo)簽: PHP 環(huán)境搭建
分享:
評(píng)論:
你還沒有登錄,請(qǐng)先