Jump to content

Techniques for Calling Methods


iwato

Recommended Posts

PROBLEM: Please find below a code snippet that includes an implementation of the call_user_func_array() function embedded inside a magic __call() method. Notice how the call_user_func_array() calls the method -- not the methods arguments.

  • the name of the class instance used to call the method, and
  • the string-name of the method that is called.

QUESTION: Is this standard calling procedure for all methods? Or, does it only apply to the special case of the call_user_func_array() function.In other words, can I, wherever the string-name of a function is normally entered into a function, enter in its place an array consisting of a class-object variable and the string-name of the method that I wish to call.DISCLAIMER: I am not asking about the general procedure for calling methods; rather, I am asking about the general procedure for calling methods inside other functions.

abstract class BubbleMethod {	public $objOwner;	function __call($sMethod, $aParams) {		if (isset($this->objOwner)) {			return call_user_func_array(array($this->objOwner, $sMethod), $aParams);		} else {			echo 'Owner for ' . get_class($this) . ' not assigned.';		}	}}

Please provide a different example using a different function, so that I am sure that we are on the same wave-length.Your dedicated student,Roddy

Link to comment
Share on other sites

In other words, can I, wherever the string-name of a function is normally entered into a function, enter in its place an array consisting of a class-object variable and the string-name of the method that I wish to call.
The manual shows exactly that:
<?phpfunction foobar($arg, $arg2) {	echo __FUNCTION__, " got $arg and $arg2\n";}class foo {	function bar($arg, $arg2) {		echo __METHOD__, " got $arg and $arg2\n";	}}// Call the foobar() function with 2 argumentscall_user_func_array("foobar", array("one", "two"));// Call the $foo->bar() method with 2 arguments$foo = new foo;call_user_func_array(array($foo, "bar"), array("three", "four"));?>

Your example is a little abstract because you're using variables for everything, but yeah you can send it an array consisting of an object instance in the first element and a string method name in the second element, and it will run that method on that object. Here's a much more specific example, although you would never use select_db like this:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'test');call_user_func_array(array($mysqli, 'select_db'), 'test2');

Link to comment
Share on other sites

The manual shows exactly that:
Unfortunately, the manual "shows exactly that" only for the call_user_array_func() function, but it does not address the issue in my disclaimer. I would like to know whether this procedure for calling methods is good for all functions that call functions.The following is an example of more of the same and differs little from my own example as it uses the same function -- namely, call_user_func_array().
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'test');call_user_func_array(array($mysqli, 'select_db'), 'test2');

Roddy

Link to comment
Share on other sites

The first argument of call_user_func_array() can be an array with two members (object/class as a string, method as a string) or a single string (function/static method name). The second argument is an array of argument values.Everything else is pretty much an error.In your example, this will work for any method that can be called on the object inside $this->objOwner. If that object happens to have a __call method, then I believe (I haven't tried it, I admit) that the __call method will be called accordingly in case there's no appropriate method.I'm not completely sure what you're asking

QUESTION: Is this standard calling procedure for all methods?
It's not a standard anything. It's a PHP only way of calling any function or method.
Link to comment
Share on other sites

This seems like a question about call_user_func_array. When you're using call_user_func_array, those are the rules. If you're not using call_user_func_array, then those rules don't necessarily apply. If that doesn't answer your question, please be more specific. The only time you use an array with an object instance and method name is when you're using call_user_func_array or something else that supports the same parameters.

Link to comment
Share on other sites

If you're not using call_user_func_array, then those rules don't necessarily apply. If that doesn't answer your question, please be more specific.
OK. This appears to answer my question well.Many thanks to both you and boen_robot.Roddy
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...