1

I'm reading about the call_user_func_array- function.

When you are going to call a function, let's say foobar(), with to arguments, you do like this:

call_user_func_array("foobar", array("one", "two"));

But when you are going to call a method, let's say $foo->bar, with to arguments, you do like this:

call_user_func_array(array($foo, "bar"), array("three", "four"));

As you can see, the first parameter here is an array, with the the name of the class and then the name of the method.

My question is why you send it like an array when you want to call a method?

1
  • Why not? They're just arguments to a function as far as PHP is concerned. There's a ton of ways to get the info from the array and assign it to whatever it needs. Commented Mar 12, 2015 at 13:53

1 Answer 1

1

Because to call a method you also have to specify the object that the method is being called on. If you were calling it normally, it would be:

$foo->bar("three", "four");

When using call_user_func_array, you need to be able to specify both $foo and bar. Putting them in an array is how you indicate that this is a method call rather than an ordinary function call.

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.