0

I need to run correctly something like this:

$anchor->get_by_number(49)->set_value("hello");

Where the object name, methods, and parameters are variables:

$object = 'anchor';
$method1 = 'get_by_number';
$params1 = array('49');
$method2 = 'set_value';
$params2 = array('hello');

Using call_user_func_array, or maybe somebody knows the alternatives.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87

1 Answers1

0

As of PHP 5.6 you can use argument unpacking ...:

${$object}->$method1(...$params1)->$method2(...$params2);

To do it with call_user_func_array, just nest them:

call_user_func_array(array(call_user_func_array(array(${$object}, $method1), $params1),
                                                                  $method2), $params2);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87