Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You have to use call_user_func_array instead:

$obj = new Test();
call_user_func_array(array($obj, 'testing_cronjob'), $params);
share|improve this answer
    
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! –  qwerty Oct 9 '12 at 19:31
    
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:") –  bfavaretto Oct 9 '12 at 19:57
    
True, good advice. –  qwerty Oct 9 '12 at 19:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.