calling method of object of object with call_user_func
33,721
Solution 1
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.
Solution 2
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...
Author by
stac
Updated on December 16, 2020Comments
-
stac over 3 years
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) }