从闭包返回主方法的值 [英] return a value from closure to main method
问题描述
我的闭包如下:
公共函数create(){
DB :: transaction(function (){
返回称为
的函数);
如何从 create $ c $返回调用的函数 c>?
这取决于闭包的实现,
以这个简单的例子为例:
函数事务($ inReturnable,$ returnable)
{
$ inReturnable();
return $ returnable();
}
$ value = transaction(function(){
return'hello!';;
},function(){
return'yello! ';
});
echo $ value;
//输出:yello!
那么,这怎么有用? p>
-有时您想让对象的客户端用户有权使用某些对象方法或属性,而又不影响对象上下文,则需要使用方法返回其他内容,同时又需要授予用户访问权限以执行对象/方法中的某些操作,这可以说是一种依赖注入。
函数事务($ inReturnable,$ returnable)
{
$ privateVariable ='Hello';
$ inReturnable($ privateVariable);
return $ returnable();
}
$ value = transaction(function($ privateVariable){
echo $ privateVariable。 \n;
},function(){
echo $ privateVariable; //注意:未定义的变量:privateVariable
return'yello!';
});
echo $ value;
//输出:Hello
注意:未定义变量:privateVariable
yello!
这种实现的实际示例,区别在 php
成功则返回TRUE,失败则返回FALSE。
在对每个回调函数应用
之后,返回包含array1的所有元素的数组。
它的种类是:以下只是伪代码
//想象中的array_map
数组函数实现array_map(callable $ callback,array $ array)
{
如果为true,则返回$ callback;
}
//想象中的array_walk实现
数组函数array_walk(array $ array,callable $ callback)
{
如果为true,则返回true;否则,返回true。
}
例如:
$ array = [1,2,3];
$ map = array_map(function($ value){return $ value * 3;},$ array);
print_r($ map);
$ walk = array_walk($ array,function($ value,$ key){return $ value * 3;});
//将返回真实的
print_r($ walk);
array_walk($ array,function($ value,$ key)use(& $ array){return $ array [$ key] = $ value * 3;});
print_r($ array);
这将输出:
Array([0] => 3 [1] => 6 [2] => 9)
1
Array([0] => 3 [ 1] => 6 [2] => 9)
例如:要从回调内部获取值,您可以通过引用将变量传递给回调,然后从闭包内部对其进行编辑,然后根据需要在闭包外部使用此变量,如下所示:
DB :: transaction(function()use(& $ return){
$ return ='some value!';
});
echo $ return;
结论:-
这取决于函数/方法的实现。
I have a closure as follows:
public function create(){
DB::transaction(function(){
return 'function called'
});
how can I return "function called" from create
?
this is depends on the implementation of the closure ,
take this quick example:
function transaction($inReturnable, $returnable)
{
$inReturnable();
return $returnable();
}
$value = transaction(function() {
return 'hello!';
}, function() {
return 'yello!';
});
echo $value;
// Output: yello!
so, how this could be useful ?
-- some time you want give the client user of your object the power to use some of your object methods or properties without any effect on your object context, you need your method to return something else , in the same time that you need to give the user access to do something within your object/method, it's a kind of dependency injection so to speak .
function transaction($inReturnable, $returnable)
{
$privateVariable = 'Hello';
$inReturnable($privateVariable);
return $returnable();
}
$value = transaction(function($privateVariable) {
echo $privateVariable . "\n";
}, function() {
echo $privateVariable; // Notice: Undefined variable: privateVariable
return 'yello!';
});
echo $value;
// Output: Hello
Notice: Undefined variable: privateVariable
yello!
live example for this kind of implementing, the difference between array_map and array_walk internal functions in php
Returns TRUE on success or FALSE on failure.
Returns an array containing all the elements of array1 after applying the callback function to each one.
it's kind of : the following is just a pseudo-code
// imagined implementation of array_map
array function array_map(callable $callback, array $array)
{
return $callback if true;
}
// imagined implementation of array_walk
array function array_walk(array $array, callable $callback)
{
return true if true;
}
for example:
$array = [1,2,3];
$map = array_map(function($value) {return $value * 3;}, $array);
print_r($map);
$walk = array_walk($array, function($value, $key) {return $value * 3;});
// will return true
print_r($walk);
array_walk($array, function($value, $key) use (&$array) {return $array[$key] = $value * 3;});
print_r($array);
this will output:
Array ( [0] => 3 [1] => 6 [2] => 9 )
1
Array ( [0] => 3 [1] => 6 [2] => 9 )
for your example : to get the value from inside the callback you may pass a variable to your callback by reference then edit it's value from inside the closure then use this variable outside the closure as you want as follows:
DB::transaction(function() use (&$return) {
$return = 'some value!';
});
echo $return;
Conclusion :-
this is depends on the implementation of the function/method.
这篇关于从闭包返回主方法的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!