: This article describes how to learn ThinkPHP source code. For more information about PHP tutorials, see. A newcomer to PHP, recently working on a project, uses ThinkPHP. I want to learn from it in depth, learn the source code of ThinkPHP, and take notes to record these easy-to-forget ThinkPHP items, not much nonsense. start.
Official website description:
The I method is a new member of many single-letter functions in ThinkPHP. It is named after the English Input. it is mainly used to conveniently and securely obtain system Input variables and can be used anywhere, the format is as follows:
I ('variable type. variable name', ['default'], ['filtering method'])
The variable type refers to the request method or input type, including:
Variable type |
Description |
Get |
GET parameters |
Post |
Get POST parameters |
Param |
GET, POST, or PUT parameters for automatically determining request types |
Request |
Get request parameters |
Put |
Get PUT parameters |
Session |
Get $ _ SESSION parameters |
Cookie |
Get $ _ COOKIE parameters |
Server |
Get $ _ SERVER parameters |
Globals |
Get $ GLOBALS parameters |
Note: The variable type is case insensitive.
Variable names are case sensitive.
The default value and filtering method are optional.
The official code is as follows:
Function I ($ name, $ default = '', $ filter = null, $ datas = null ){
Static $ _ PUT = null; // use static to define a static state. if the declared class member or method is static, you can directly access the object without instantiating the class. A static member cannot be accessed through an object (except for static methods)
If (strpos ($ name, '/') {// specify the modifier strpos () function to find the position where the string first appears in another string, find the location where '/' appears for the first time in the nam parameter
List ($ name, $ type) = explode ('/', $ name, 2); // explode (separator, string, limit) function means to scatter strings as arrays based on specific characters, and limit indicates the number of returned arrays.
} Elseif (C ('Var _ AUTO_STRING ') {// it is forcibly converted to a string by default. // The C method of ThinkPHP is called.
$ Type ='s ';
}
// Conclusion: if indicates whether the parameter has/
If (strpos ($ name, '.') {// specify the parameter source // check whether it contains .!
List ($ method, $ name) = explode ('.', $ name, 2 );
} Else {// The default value is automatic judgment.
$ Method = 'param ';
}
Switch (strtolower ($ method) {// strtolower () converts all characters to lowercase. use switch to locate the type of the method.
Case 'get ':
$ Input = & $ _ GET;
Break;
Case 'Post ':
$ Input = & $ _ POST;
Break;
Case 'put ':
If (is_null ($ _ PUT )){
Parse_str (file_get_contents ('php: // input'), $ _ PUT );
}
$ Input = $ _ PUT;
Break;
Case 'param ':
Switch ($ _ SERVER ['request _ method']) {// $ _ SERVER ['request _ method' obtains the request method using the obtained METHOD name, using swith () to locate the type of the method. The concept here is recursion.
Case 'Post ':
$ Input = $ _ POST;
Break;
Case 'put ':
If (is_null ($ _ PUT )){
Parse_str (file_get_contents ('php: // input'), $ _ PUT );
}
$ Input = $ _ PUT;
Break;
Default:
$ Input = $ _ GET;
}
Break;
Case 'path ':
$ Input = array ();
If (! Empty ($ _ SERVER ['path _ info']) {
$ Depr = C ('URL _ PATHINFO_DEPR ');
$ Input = explode ($ depr, trim ($ _ SERVER ['path _ info'], $ depr ));
}
Break;
Case 'request ':
$ Input = & $ _ REQUEST;
Break;
Case 'session ':
$ Input = & $ _ SESSION;
Break;
Case 'cookies ':
$ Input = & $ _ COOKIE;
Break;
Case 'server ':
$ Input = & $ _ SERVER;
Break;
Case 'globals ':
$ Input = & $ GLOBALS;
Break;
Case 'data ':
$ Input = & $ datas;
Break;
Default:
Return null;
}
If (''= $ name) {// get all variables
$ Data = $ input;
$ Filters = isset ($ filter )? $ Filter: C ('default _ filter'); // The isset () function is used to check whether the variable is set. empty () is used to determine whether it is null.
If ($ filters ){
If (is_string ($ filters )){
$ Filters = explode (',', $ filters );
}
Foreach ($ filters as $ filter ){
$ Data = array_map_recursive ($ filter, $ data); // The parameter filtering method array_map_recursive foreach () of ThinkPHP is a cyclic function.
}
}
} Elseif (isset ($ input [$ name]) {// value operation
$ Data = $ input [$ name];
$ Filters = isset ($ filter )? $ Filter: C ('default _ filter ');
If ($ filters ){
If (is_string ($ filters )){
If (0 === strpos ($ filters ,'/')){
If (1! = Preg_match ($ filters, (string) $ data) {// preg_match () is used to match a regular expression.
// Supports regular expression verification.
Return isset ($ default )? $ Default: null;
}
} Else {
$ Filters = explode (',', $ filters );
}
} Elseif (is_int ($ filters )){
$ Filters = array ($ filters );
}
If (is_array ($ filters )){
Foreach ($ filters as $ filter ){
If (function_exists ($ filter )){
$ Data = is_array ($ data )? Array_map_recursive ($ filter, $ data): $ filter ($ data); // filter parameters
} Else {
$ Data = filter_var ($ data, is_int ($ filter )? $ Filter: filter_id ($ filter ));
If (false ===$ data ){
Return isset ($ default )? $ Default: null;
}
}
}
}
}
If (! Empty ($ type) {// use switch to determine the type
Switch (strtolower ($ type )){
Case 'a': // Array
$ Data = (array) $ data;
Break;
Case 'D': // number
$ Data = (int) $ data;
Break;
Case 'F': // floating point
$ Data = (float) $ data;
Break;
Case 'B': // boolean
$ Data = (boolean) $ data;
Break;
Case's ': // string
Default:
$ Data = (string) $ data;
}
}
} Else {// Default Variable value
$ Data = isset ($ default )? $ Default: null;
}
Is_array ($ data) & array_walk_recursive ($ data, 'think _ filter ');
Return $ data;
}
The I method is mainly used to obtain the value sent from the foreground to the background. the method defines four parameters. only one parameter is required. this parameter is the name to be obtained. Other parameters can be changed.
The above marked red is to remember some knowledge points during the code process.
Function: securely obtain input parameters.
Implementation ideas:
If the type is retained during submission, the processing is based on the specified type. if the type is not retained with the specified type, the type is determined based on the server variable.
Filter to ensure the security of submitted data.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the ThinkPHP source code learning method I, including the content, hope to be helpful to friends who are interested in PHP tutorials.