php use闭包参数,php 闭包use的使用

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

PHP的闭包即为匿名函数。

示例如下。

$message = 'hello';

// 继承 $message

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

var_dump($message);

};

echo $example(); //hello

// Inherited variable's value is from when the function

// is defined, not when called

//use继承的变量值是函数定义时的变量值,而不是调用时的变量值

$message = 'world';

echo $example(); //hello

// Reset message

$message = 'hello2';

// Inherit by-reference

//通过引用继承,则可以获取到变量的变化值。

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

var_dump($message);

};

echo $example(); //hello2 引用地址

// The changed value in the parent scope

// is reflected inside the function call

$message = 'world';

echo $example(); //world 引用地址

// Closures can also accept regular arguments

//重新定义闭包

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

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

};

$example("hello"); //hello world

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值