du997562 2016-07-16 09:25
浏览 54
已采纳

将函数结果存储到变量以供以后在PHP中使用?

Considering the time I've been using PHP I feel shame for asking this, but it seems I'm way too tired to solve it by myself at this moment.

I have a php variable function that gets an CSV file and stores the data into an array.

$csvdata = function(){
    // Get CSV data to array
    return $array;
}

This array is used by other functions in different situations.

The thing is that I though this functions will run just once, but I just noticed it runs every time I use the var $csvdata.

What I want is to create a self evoked function that runs only once and store the data into an array that I can use as many times as needed without PHP re-doing the process of getting the csv and storing the data every time I use $csvdata.

Its really weird because I feel like if my brain is playing a joke on me... (Brain: "It's obvious... I know the answer but I wont tell you")

Sorry pals

  • 写回答

2条回答 默认 最新

  • douyi1084 2016-07-16 09:31
    关注

    You can call newly created functions on the fly with call_user_func:

    $csvdata = call_user_func(function(){
        // Get CSV data to array
        return $array;
    });
    

    Then just reference $csvdata as a variable (without parenthesis).

    Explanation

    The documentation calls the argument to call_user_func a callback, and that is what it is: you pass a function reference to it, and it is the implementation of call_user_func that calls your function for you ("calling you back"). The function reference can be an inline, anonymous function (like above).

    But it can also be a variable representing a function:

    $func = function(){
        // Get CSV data to array
        return $array;
    };
    $csvdata = call_user_func($func);
    

    Or, it can be a string, which holds the name of the function:

    function myFunc(){
        // Get CSV data to array
        return $array;
    };
    $csvdata = call_user_func('myFunc');
    

    All of these do the same: your function gets called. You can even tell call_user_func which arguments to pass to your function:

    function myFunc($arg){
        echo $arg;
        // Get CSV data to array
        return $array;
    };
    $csvdata = call_user_func('myFunc', 'hello');
    

    The above will echo 'hello'.

    I provide these variants just for information, as your interest is in the inline function argument. Also then you can pass arguments:

    $csvdata = call_user_func(function($arg1, $arg2){
        echo $arg1 . "," . $arg2;
        // Get CSV data to array
        return $array;
    }, 'hello', 'there');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?