The official manual provides the following examples:
CopyCode The Code is as follows: <? PHP
// Example of using namespace
Namespace foobar;
Class Foo {
Static public function test (){
Print "Hello world! \ N ";
}
}
Call_user_func (_ namespace _. '\ FOO: test'); // as of PHP 5.3.0
// Hello world!
Call_user_func (Array (_ namespace _. '\ foo', 'test'); // as of PHP 5.3.0
// Hello world!
?>
Copy codeThe Code is as follows: <? PHP
// Example of directly calling a method
Class myclass {
Static function say_hello ()
{
Echo "Hello! \ N ";
}
}
$ Classname = "myclass ";
Call_user_func (Array ($ classname, 'Say _ hello '));
Call_user_func ($ classname. ': say_hello'); // as of 5.2.3
?>
So what if it is a common method and the method has parameters?
The following is a small example written by the author for your reference:Copy codeThe Code is as follows: <? PHP
// Execute a class with Parameters
Class loveapple {
Public Function sayhello ($ A, $ B ){
Echo "Hello:". $ A. ".". $ B. "\ n ";
}
}
$ OBJ = new loveapple ();
// Execution result Hello: loveapple. Using instance.
Call_user_func (Array ($ OBJ, "sayhello"), "loveapple", "using instance .");
// Execution result Hello: loveapple. Using class name.
Call_user_func (Array ("loveapple", "sayhello"), "loveapple", "using class name .");
?>