In PHP, you can use Call_user_func to make dynamic calls to methods, and to invoke normal functions, class methods, and class methods with parameters dynamically.
1. Define a common function getcurrentdate, which is used to get today's date.
The parameter on the Call_user_func band is the name of the function to be called
Fucntion Getcurrentdate () {
Echo ' Getcurrentdate: '. Date (' y-m-d ');
}
Call_user_func (' getcurrentdate ');
The program automatically executes the Getcurrentdate function and obtains the desired result
Getcurrentdate:2016-04-13
2. Define a class Cls53 and class method gettitle,call_user_func the input parameter becomes an array, the first element of the array is the object name, the second element is the class method name.
Class cls53{
Public Function GetTitle () {
Echo ' title ';
}
}
$cls = new Cls53 ();
Call_user_func (Array ($cls, ' getTitle '));
The program automatically calls the object CLS method GetTitle and obtains the desired result
Title
3. A method with parameters can also be called, at which time the GetTitle method is changed to GetTitle ($title).
When called, the second argument is the one that needs to be passed to the method
Class cls53{
Public Function GetTitle ($title) {
echo $title;
}
}
$cls = new Cls53 ();
Call_user_func (Array ($cls, ' getTitle '), ' abc ');
The incoming parameter is ABC, which results in the desired result:
Abc
How to use Call_user_func dynamic call method in PHP