0

Not quite sure how to word the title better.

What i need to do, is call a method with call_user_func. The method i'm calling accept several different variables (var1, var2, var3 etc), not an array. The problem is, when calling the function i need to pass the array i have as seperate variables to the method. Let me show you.

Here's my method:

class Test {

    public function testing_cronjob($p1, $p2, $p3, $p4, $p5) {
        echo 'p1 = ' . $p1 . '<br />';
        echo 'p1 = ' . $p2 . '<br />';
        echo 'p1 = ' . $p3 . '<br />';
        echo 'p1 = ' . $p4 . '<br />';
        echo 'p1 = ' . $p5 . '<br />';
    }
}

And here's how i currently (try) to call it:

$obj = new Test();
call_user_func(array($obj, 'testing_cronjob'), $params);

The method runs, but it only gets one variable, which is not what i want.

$params look like this:

$params = array(
    0 => 'first one',
    1 => 'second one',
    2 => 'third param!',
    3 => 'oh and im fourth!!',
    4 => 'im last, fml! :('
)

The way i got myself into this situation, is because i'm creating a cronjob model. It accepts params along with class and method name. It's all stored in a database, and when it runs, i need to be able to send the $params to the method as intended.

I can't rewrite how methods accept variables, because that would be way too much work. I'm going to be calling several (10+) methods from the cronjob, all of which are pretty well used in the application. I'm also not sure how i would re-write it, but if you have any suggestions about that, let me know.

Also, i'm using codeigniter if that matters. If i forgot to mention anything or you want more code, just drop a comment.

Is this possible? Are there any workarounds? Any suggestions are welcome!

1

1 Answer 1

1

You have to use call_user_func_array instead:

$obj = new Test();
call_user_func_array(array($obj, 'testing_cronjob'), $params);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh you have got to be F@*&/#G kidding me! Here i sat, spending hours trying to solve this, only to write a question here and get told i only need to append 6 tiny little words to the end of the function name, and voila, it works! Thanks a bunch!
Minor nitpick: always read the whole manual page for the PHP functions you're having trouble with. The page on call_user_func includes a link to call_user_func_array on the bottom ("see also:")

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.