ThinkPHP automatically completes the use of functions and callback method instances, thinkphp callback _ PHP Tutorial

Source: Internet
Author: User
ThinkPHP automatically completes the use of functions and callback method instances, thinkphp callback. ThinkPHP uses the function and callback method when it is automatically completed. thinkphp calls back the example in this article to describe how to use the function and callback method when ThinkPHP is automatically completed. Share it with you for your reference. ThinkPHP automatically completes the use of functions and callback method instances, thinkphp callback

This example describes how to use functions and callback methods when ThinkPHP is automatically completed. Share it with you for your reference. The specific method is as follows:

The automatic filling format of ThinkPHP is as follows:

The code is as follows:

Array (fill field, fill content [, fill condition] [, add rules])

Additional rules, optional, including:

String: string, indicating that the filled content is a string (default ).

Function: indicates that the filled content is a function return value.

Callback: indicates that the filled content is the return value of the method of the current Model.

Field: field, indicating that the filled content is the value of another field.

ThinkPHP auto-fill using function

When an additional rule is filled with a function, it indicates 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:

The code is as follows:

Class UserModel extends Model {
Protected $ _ auto = array (
// Use the md5 function to process the password field in all circumstances
Array ('password', 'md5', 3, 'Function '),
// Write the current timestamp to the newly added regdate field
Array ('regdate', 'time', 1, 'Function '),
// Write the user-registered IP address to the regip field when it is added.
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 preceding example, md5 and time are used as built-in PHP functions, and the result of filling is md5 ($ _ POST ['password']) and time () function values, get_client_ip and get_name are Common and common. php user-defined functions.

The get_name function adds the user name with the th _ prefix,Refer to the following:

The code is as follows:

Function get_name ($ name ){
Return 'th _ '. $ name;
}


If the function requires a parameter, the filling field is used as the parameter, and the md5 and get_name functions above are filled.

Call back for automatic ThinkPHP filling

When the method callback is used for filling, it indicates that the filled content is the return value of the current Model method. the example of using callback for filling is as follows:

The code is as follows:

Class UserModel extends Model {
Protected $ _ auto = array (
// Call back the getName method when adding the username field
Array ('username', 'getname', 1, 'callback '),
);
}


The getName method adds the th _ prefix to the user name.For more information, see:

The code is as follows:

Class UserModel extends Model {
// Add the entered username with the th _ prefix
Function getName (){
Return 'th _ '. $ _ POST ['username'];
}
}


Note: In the preceding example, the username field is automatically prefixed with th _ and filled with username. this is only used to demonstrate the usage of auto-filling using functions or callback methods, which may not be of practical production significance.

I hope this article will help you with ThinkPHP framework programming.

Examples in this article describes how to use functions and callback methods when ThinkPHP is automatically completed. Share it with you for your reference. With...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.