Bryan
Bryan

Reputation: 3541

call_user_func_array on methods in classes

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?

Upvotes: 1

Views: 108

Answers (1)

Barmar
Barmar

Reputation: 782693

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.

Upvotes: 1

Related Questions