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
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.
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With