Thinkphp using functions and callback method instances in auto-completion _php
Source: Internet
Author: User
Keywordsthinkphp auto-complete function callback square
This article describes the use of functions and callback methods in thinkphp auto-completion. Share to everyone for your reference. Here's how:
The thinkphp auto-fill format is as follows:
The code is as follows:
Array (fill field, fill content [, fill condition] [, additional rules])
Additional rules, optional, include:
String that represents the fill content as a string (default).
Functions: A function is used to indicate that the content of a fill is a function return value.
Callback: The method used to indicate that the content of the fill is a method return value for the current Model.
Field: The value that indicates that the filled content is one of the other fields.
thinkphp Auto-fill using function functions
When additional rules are populated with function functions, the content that represents the fill is a function return value, which can be a PHP built-in function or a user-defined function.
Use the function to populate the example:
The code is as follows:
Class Usermodel extends model{ Protected $_auto = Array ( Use the MD5 function with the password field in all cases Array (' Password ', ' MD5 ', 3, ' function '), Writes the current timestamp to the RegDate field when new Array (' regdate ', ' time ', 1, ' function '), Write user registered IP address to REGIP field when new Array (' Regip ', ' get_client_ip ', 1, ' function '), Use the custom GetName function for the username field when new Array (' username ', ' get_name ', 1, ' function '), ); }
In the example above, the MD5 and time used for PHP built-in functions, the result is MD5 ($_post[' password ') value and time () function value, GET_CLIENT_IP and Get_name are common/common.php Custom functions.
the Get_name function adds the user name to the th_ prefix, as follows:
The code is as follows:
function Get_name ($name) { Return ' Th_ '. $name; }
If the function requires parameters, the fields are populated as parameters, such as the MD5 and get_name functions above.
thinkphp Auto-fill using method callback
When using the method callback fill, the content that represents the fill is a method return value for the current Model, using the callback fill example:
The code is as follows:
Class Usermodel extends model{ Protected $_auto = Array ( Callback GetName method for username field when new Array (' username ', ' getName ', 1, ' callback '), ); }
the GetName method adds the user name to the th_ prefix, refer to the following:
The code is as follows:
Class Usermodel extends model{ Append the incoming username to the th_ prefix function GetName () { Return ' th_ '. $_post[' username ']; } }
Note: The above example populates the username with the th_ prefix before the username field, only to illustrate the use of autofill using functions or callback methods, which may not be of practical production significance
It is hoped that this article will be helpful to everyone's thinkphp framework design.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.