Examples of functions and callback methods used in ThinkPHP auto-completion, thinkphp callbacks
The examples in this article describe the use of functions and callback methods in ThinkPHP auto-completion. Share it with everyone for your reference. The specific method is as follows:
ThinkPHP autofill format is as follows:
Copy code The code is as follows:
array(fill field, fill content[, fill condition][, additional rules])
Additional rules, optional, including:
string: String, indicating that the filling content is a string (default).
function: Use function to indicate that the filled content is a function return value.
callback: Use method to indicate that the filled content is the method return value of the current Model.
field: Field, indicating that the filled content is the value of another field.
ThinkPHP autofill uses function function
When additional rules are filled using a function, it means that the filled content is a function return value. This function can be a PHP built-in function or a user-defined function.
Example of using function filling:
Copy code The code is as follows:
class UserModel extends Model{
protected $_auto = array (
// Use the md5 function to process the password field in all cases
array('password','md5',3,'function'),
// Write the current timestamp to the regdate field when adding it
array('regdate','time',1,'function'),
// Write the user's registered IP address when adding the regip field
array('regip','get_client_ip',1,'function'),
// Use the custom getName function when adding the username field
array('username','get_name',1,'function'),
);
}
In the above example, the md5 and time used are PHP built-in functions, the filling results are the md5($_POST['password']) value and the time() function value, and get_client_ip and get_name are Common/common.php custom functions.
The get_name function adds th_ prefix to the username, Reference is as follows:
Copy code The code is as follows:
function get_name($name){
return 'th_'.$name;
}
If the function requires parameters, fill the fields as parameters, such as the md5 and get_name functions above.
ThinkPHP autofill usage callback
When using the callback method to fill, it means that the filled content is the method return value of the current Model. Example of using callback to fill:
Copy code The code is as follows:
class UserModel extends Model{
protected $_auto = array (
// Call back the getName method when the username field is added
array('username','getName',1,'callback'),
);
}
The getName method adds the th_ prefix to the user name. The reference is as follows:
Copy code The code is as follows:
class UserModel extends Model{
//Add th_ prefix to the passed in username
function getName(){
return 'th_'.$_POST['username'];
}
}
Note: The above example automatically adds the th_ prefix to the username field and fills it into username. This is only to illustrate the use of automatic filling functions or callback methods, and may not have actual production significance
I hope this article will be helpful to everyone’s ThinkPHP framework programming.
http://www.bkjia.com/PHPjc/919628.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/919628.htmlTechArticleExamples of functions and callback methods used in ThinkPHP auto-completion, thinkphp callbacks This article describes the use of functions and callback methods in ThinkPHP auto-completion callback method. Share it with everyone for your reference. Tool...