consider this simple scenario:
$this->method($arg1, $arg2);
Solution:
call_user_func_array(array($this,'method'), array($arg1, $arg2));
consider this scenario:
$this->object->method($arg1, $arg2);
Should this solution work?
call_user_func_array(array($this->object,'method'), array($arg1, $arg2));
Or should this work?
call_user_func_array(array($this, 'object','method'), array($arg1, $arg2));
Edit: Will try/catch works for SOAP exception, triger while using call_user_func?
try { $soap_res = call_user_func_array(array($this->service,'getBanana'), array(0, 10)); } catch (SoapFault $fault) { die($fault->faultstring) }
The call_user_func() is an inbuilt function in PHP which is used to call the callback given by the first parameter and passes the remaining parameters as argument. It is used to call the user-defined functions.
The call_user_func_array() function is a special way to call an existing PHP function. It takes a function to call as its first parameter, then takes an array of parameters as its second parameter.
Instead of executing the function in an array you can directly assign to some variable and call the function and pass the arguments, then you can use that assigned variable inside your array.
This should work:
call_user_func_array(array($this->object,'method'), array($arg1, $arg2));
The first argument is a callback type, containing an object reference and a method name.
Here's a hackish variant, might be useful to someone:
$method_name_as_string = 'method_name'; $this->$method_name_as_string($arg1, $arg2);
This uses the PHP variable-variables. Ugly as hell, but not particularly uglier than the others...
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