Home  >  Article  >  Backend Development  >  ThinkPHP source code learning method

ThinkPHP source code learning method

WBOY
WBOYOriginal
2016-07-29 09:16:161165browse

I am new to PHP. I am working on a project recently, using ThinkPHP. I want to learn more deeply, so I deliberately studied the source code of ThinkPHP and took notes to record these easily forgotten things. Without further ado, let’s get started.

Official website description:

I method is a new member of ThinkPHP’s many single-letter functions. Its name comes from the English Input (input). It is mainly used to obtain system input variables more conveniently and safely. It can be used Anywhere, the usage format is as follows:

I('Variable type.Variable name',['Default value'],['Filter method'])

Variable type refers to the request method or input type, including:

Variable type Meaning
get Get GET parameters
post Get POST parameters
param Automatically determine the request type Get GET, POST or PUT parameters
request get REQUEST parameter
put get PUT parameter
session get $_SESSION parameter
cookie get$_ COOKIE PARAMETERS
server GET$ _SERVER parameter
globals Get $GLOBALS parameter

Note: Variable types are not case-sensitive.
Variable names are strictly case-sensitive.
Default value and filtering method are optional parameters.

The official code is as follows:

function I($name,$default='',$filter=null,$datas=null) {
static $_PUT=null;//use static to define a static and declare class members Or if the method is static, it can be accessed directly without instantiating the class. Static members cannot be accessed through an object (except static methods)
if(strpos($name,'/')){ // Specify the modifier strpos() function to find a string in another character The first occurrence position in the string, find the first occurrence position of '/' in parameter nam
list($name,$type) =explode('/',$name,2 );//explode(separator,string,limit) function meaning is to spread the string into an array based on specific characters, limit is the number of returned arrays
}elseif(C('VAR_AUTO_STRING')){ // Forced to string by default // Called ThinkPHP's C method
          $type   =   's';
  }

//Summary The meaning of this if is to determine whether the parameter contains /
if(strpos($name,'.')) { //Specify the parameter source //Check whether it contains.!M List ($ Method, $ name) = Explode ('.', $ Name, 2); } Else {// The default is automatically judged
$ method = 'param'; )) {
//strtolower() Convert all characters to lowercase and use switch to locate the type of the method

         case 'get'                                                                                post' :
                                                                                             parse_str(file_get_contents('php://input'), $ _PUT);
                                                                                                                                                      
            switch($_SERVER['REQUEST_METHOD']) { //$_SERVER[' REQUEST_METHOD' Get the requested method name and use swith() to locate the method type. The idea here is recursion
              case 'POST':                                                   ’ ’ s ’ s ’ ’ s ’ s ’ s ‐ ‐ ‐ ‐ ​ to ​break; case 'PUT' :
                                                                                 
)
                                                                                                                                                             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 ' cookie' : $input =& $_COOKIE; break ;
           case 'server'                                                                                      put =& $GLOBALS; break;
case 'data' :
$input =& $datas;
break;
default:
return null;
}
if(''==$name) { // Get all variables
$data = $input;
$filters = isset($filter)?$filter:C('DEFAULT_FILTER');//Used trinocular The operator isset() function is generally used to detect whether a variable is set, and empty() is used to determine whether it is empty. $ Filters); FOREACH () is the cycle function
}}
}elseif(isset($input[$name])) { // Value operation
      $data                                                                                                              ; If ($ Filters) {
IF (IS_STRING ($ Filters)) {
IF (0 === Strpos ($ FILTERS, '/')) {
if (1! == Preg_match ($ Filters, (String )$data)){ //preg_match() is used to match regular expressions
                                                                                                                                                                                                                                                                                                                                                                                                                ',',$filters);                                                                                                                                                                                                                                                                                                                             $filter){
                                                                                                                                                                                                                                                                                                                                                                      }else{
                                             data = filter_var($data,is_int($filter) ? $filter : filter_id($filter));
                                                                                                                                      }
                                                                                            
                                                                   $data
=
(array)$data;                                                                                                            $data; break;
case 'f':// Floating point                                                                                                                                                  
// Boolean
$data =(boolean)$data;
           break; data = (string)$data; } }
}else { // Variable default value         $data                                               using using use using using ’ ‐             ‐     ‐                                                                 together using ’ ’ s ’ through ’s ’s to ensue d--   ‐ ‐                                                             The I method is mainly used to obtain the value passed from the front to the back. The method defines four parameters, only one of which is required. This parameter is the name to be obtained, and the others can be changed.
The ones marked in red above are some knowledge points that you should remember when reading the code. Function: Safely obtain parameters passed by inputImplementation idea:
If the type is followed when submitting, it will be processed according to the specified type. If it is not kept up with the specified type, the type will be determined based on the server variable. Ensure the security of submitted data through filtering. Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the method of learning ThinkPHP source code, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Object-oriented PHP (5)Next article:Object-oriented PHP (5)