Recommended replacement for deprecated call_user_method? Recommended replacement for deprecated call_user_method? php php

Recommended replacement for deprecated call_user_method?


As you said call_user_func can easily duplicate the behavior of this function. What's the problem?

The call_user_method page even lists it as the alternative:

<?phpcall_user_func(array($obj, $method_name), $parameter /* , ... */);call_user_func(array(&$obj, $method_name), $parameter /* , ... */); // PHP 4?>

As far as to why this was deprecated, this posting explains it:

This is because the call_user_method() and call_user_method_array() functions can easily be duplicated by:

old way:
call_user_method($func, $obj, "method", "args", "go", "here");

new way:
call_user_func(array(&$obj, "method"), "method", "args", "go", "here");

Personally, I'd probably go with the variable variables suggestion posted by Chad.


You could do it using variable variables, this looks the cleanest to me. Instead of:

call_user_func(array($obj, $method_name), $parameter);

You do:

$obj->{$method_name}($parameter);


Do something like that :

I use something like that in my __construct() method.

$params = array('a','b','c'); // PUT YOUR PARAMS IN $params DYNAMICALLYcall_user_func_array(array($this, $this->_request), array($params));

1st argument : Obect instance, 2nd argument : method to call, 3rd argument : params

Before you can test if method or Class too, with :

method_exists()class_exists()

Cheers