thinkphp function I code optimization

Source: Internet
Author: User

thinkphp/common/common.php file

I function, mainly used to obtain some of the GPC request variables, the function is a part of the Code filter variables, run once each time, in fact, is not necessary.

If you call it this way every time: $a = i (' post.a '), $c = i (' post.b '), $c = i (' post.c '), and the filter function will filter the $_post data 3 times, considering that the function is frequently called and can be optimized.

Optimization scheme, add a static variable, the first run after the data storage, subsequent calls directly out of the can, the code is as follows:

By ZXM I modified the code part of function I ($name, $default = ", $filter =null) {Static $maps = array ();//by zxm if (Strpos ($name, '. ')    ) {//Specify the parameter source list ($method, $name) = Explode ('. ', $name, 2);    }else{//default is automatic judgment $method = ' param '; } if (!isset ($maps [$method])) {//by ZXM switch (strtolower ($method)) {case ' get ': $input =& $_ge        T;break;        Case ' post ': $input =& $_post;break;        Case ' put ': Parse_str (file_get_contents (' Php://input '), $input); Case ' param ': switch ($_server[' Request_method ')} {case ' POST ': $input = $_pos                T            Break                Case ' PUT ': Parse_str (file_get_contents (' Php://input '), $input);            Break            Default: $input = $_get; } if (C (' var_url_params ') && isset ($_get[c (' Var_url_params ')]) {$input = Array_merge ( $input, $_get[c (' Var_url_params ')]);           } break;   Case ' request ': $input =& $_request;            Break   Case ' Session ': $input =& $_session;            Break    Case ' cookie ': $input =& $_cookie;            Break    Case ' server ': $input =& $_server;            Break    Case ' globals ': $input =& $GLOBALS;            Break        Default:return NULL;        }//Global filter//array_walk_recursive ($input, ' filter_exp ');            if (c (' var_filters ')) {$_filters = explode (', ', C (' var_filters '));            foreach ($_filters as $_filter) {//Global parameter filter array_walk_recursive ($input, $_filter); }} $maps [$method] = $input;//by zxm}else{$input = $maps [$method];//by ZXM} if (Empty ($na         Me) {//Get all variables $data = $input;        $filters = Isset ($filter)? $filter: C (' default_filter '); if ($filters) {$filters = explode (', ', $filters);            foreach ($filters as $filter) {$data = Array_map ($filter, $data);//Parameter Filtering}}        }elseif (Isset ($input [$name])) {//Take value operation $data = $input [$name];        $filters = Isset ($filter)? $filter: C (' default_filter ');            if ($filters) {$filters = explode (', ', $filters); foreach ($filters as $filter) {if (function_exists ($filter)) {$data = Is_array ($data) ? Array_map ($filter, $data): $filter ($data);                     Parameter filtering}else{$data = Filter_var ($data, Is_int ($filter)? $filter: filter_id ($filter));                    if (false = = = $data) {return isset ($default)? $default: NULL;    }}}}}else{//variable default value $data = Isset ($default)? $default: NULL; } return $data;}

  

thinkphp function I code optimization

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.