1

I have a similar question to this one : PHP can I call unexistent function with call_user_func

I need to call a method defined width __call but i don't know how many parameter to pass. So I am using this :

call_user_func_array(array($object, $method_name), $arguments);

But it doesn't work. Actually, this works :

$object->$method_name();

But i don't know how to pass parameter by an other way that call_user_func_array...

I wan't something like :

$object->$method_name($arguments[0], $arguments[1], $arguments[2] /*,... until no more args*/);

An idea ? Thank you

3
  • Please show us how you defined the __call() method, by editing your question, because it should just work. Commented May 22, 2017 at 14:57
  • 1
    But it doesn't work Really, what DOES it do, error, sparks, leakage??? Commented May 22, 2017 at 15:09
  • I'm so stupid... it was in order to get a value. so actually it did just nothing. Now it seems logical for me to put a return before my call_user_func... An d you know what ? it works ! Thank you very much Commented May 23, 2017 at 7:20

1 Answer 1

2

This is perfectly possible. I don't know your code, but here's a quick test I put together to make sure it works:

class Foo {
    function _bar($arg1, $arg2) {
        print_r(func_get_args());
    }

    function __call($name, $args) {
        call_user_func_array([$this, '_'.$name], $args);
    }
}

$foo = new Foo();
$func = 'bar';

call_user_func_array([$foo, $func], ['1', '2']);

// Array
// (
//     [0] => 1
//     [1] => 2
// )

This tries to call the function Foo::bar() which doesn't exist, so it ends up in Foo::__call(), which then calls Foo::_bar().

Sign up to request clarification or add additional context in comments.

Comments

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.