什么是PHP闭包???

闭包函数:临时创建一个没有名称的函数,经常作为回调函数来用。

通俗的说就是:子函数可以使用父函数中的局部变量,这种行为叫做闭包。

1、匿名函数赋值

  $demo=function($str){

    echo $str;

  }

  $demo('hello,world');

 

2、闭包可以从父作用域中继承变量,任何此类型变量都应该用use语言结构传递进去。

  $message='hello';

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

    var_dump($message);

  };

  echo $example();

  结果:hello;

 

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

    var_dump($message);

  }

  echo $example();

  结果:hello;

 

  $message='world';

  echo $example();

  结果:world;

 

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

    var_dump($arg.' '.$message);

  }

  $example('hello');

  结果:hello world;

  

转载于:https://www.cnblogs.com/sweet521/p/5623836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值