3

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

0

2 Answers 2

6

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');
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @trincot It works! Could you provide some explanation in your answer of why using call_user_func() did the trick? I was reading the documentation but it is very poor to give me an idea of why now it's running just once. Just for future reference... Thanks again!
call_user_func is used for calling the function whose name you don't know if you have a funcion with name you can call it by just calling its name
@ArunKumaresh I would be lying if I say that I understand your comment LOL
see my answer then you can understand the differnece
0

you can give a name to the function and make a call like this

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

    $csvdata=csv();

4 Comments

This wont work. Is exactly the same I want to avoid. This way every time you call $csvdata php will run csv(). In my case, this means it will load the csv file and work on it every time I use this var.
Not true, the function csv will only be called once as $csvdata will hold an array and not a closure like in your example
@luis see the difference between your code and this answer
Arun and @DarkBee you guys are correct. This is why resting is an important part of life. I though my version was somehow a shorthanded version of yours, but now I see there's a difference. I'm going to research a bit about closure to get a better understanding of when it should be used. Now I think this is the correct answer given the fact that I was wrongly using 'closure'. Thanks pals!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.