dongzha0813 2017-03-13 13:27
浏览 35
已采纳

从closure返回一个值到main方法

I have a closure as follows:

public function create(){
   DB::transaction(function(){
       return 'function called'
   });

how can I return "function called" from create?

  • 写回答

2条回答 默认 最新

  • douan4106 2017-03-13 14:34
    关注

    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 . "
    ";
    }, 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

    array_walk :

    Returns TRUE on success or FALSE on failure.

    array_map :

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵