Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call_user_func_array vs $controller->$method($params)?

Tags:

php

I 'm using this in my code:

 call_user_func_array ( array ($controller, $method ), $this->params );

but I found out that the code below does the same thing:

 $controller->$method($this->params);

Is there any difference between the two versions?

Thanks

Adam Ramadhan

like image 375
Adam Ramadhan Avatar asked Mar 20 '11 14:03

Adam Ramadhan


2 Answers

They are not the same.

If $method is showAction and $this->params is array(2, 'some-slug'), then the first call would be equivalent to:

$controller->showAction(2, 'some-slug');

Whereas the second would be:

$controller->showAction(array(2, 'some-slug'));

Which one you want to use depends on how the rest of your system works (your controllers in particular). I personally would probably go with the first.

like image 125
igorw Avatar answered Sep 27 '22 20:09

igorw


They work alike. The only significant difference is that $controller->$nonexistant() would generate a fatal error. While call_user_func_array fails with just an E_WARNING should $method not exist.

Fun fact. Should your $controller harbor a closure $method, then you would actually have to combine both approaches:

call_user_func_array ( $controller->$method, $this->params );
like image 45
mario Avatar answered Sep 27 '22 20:09

mario