Common/common. php file Common Function Analysis in ThinkPHP,

Source: Internet
Author: User
Tags autoload

Common/common. php file Common Function Analysis in ThinkPHP,

This document analyzes Common functions of ThinkPHP/common/Common. php files. We will share this with you for your reference. The details are as follows:

/*** You can obtain and set configuration parameters in batches. * @ param string | array $ name configuration variable * @ param mixed $ value configuration value * @ return mixed */function C ($ name = null, $ value = null) {static $ _ config = array (); // obtain all if (empty ($ name) {if (! Empty ($ value) & $ array = cache ('C _'. $ value) {// array_change_key_case (): change all key names in the array to lowercase or uppercase. By default, all key names are changed to lower case $ _ config = array_merge ($ _ config, array_change_key_case ($ array);} return $ _ config;} // prioritize the settings to get or assign values. If parameter 2 is blank, it is used to get the value of the configuration variable; if parameter 2 is not empty, it is the case where the value is set for the configuration variable if (is_string ($ name )) {// set the value of the configuration variable or obtain the value of the configuration variable if (! Strpos ($ name ,'. ') {// if no ". ", run the following code $ name = strtolower ($ name); // convert the configuration variable name to lowercase if (is_null ($ value )) return isset ($ _ config [$ name])? $ _ Config [$ name]: null; // when the configuration variable value is obtained, the value of the configuration variable is returned. $ _ config [$ name] = $ value; // return when setting values for configuration variables;} // supports $ name = explode ('. ', $ name); $ name [0] = strtolower ($ name [0]); if (is_null ($ value )) return isset ($ _ config [$ name [0] [$ name [1])? $ _ Config [$ name [0] [$ name [1]: null; // obtain the value $ _ config [$ name [0] [$ name [1] = $ value; // return of the value assignment ;} // batch set if (is_array ($ name) {// batch set the value // array_change_key_case (): change all the key names in the array to lowercase or uppercase, by default, the keys are changed to lower-case letters. Note: only the key names are changed to lower-case letters. $ _ config = array_merge ($ _ config, array_change_key_case ($ name); if (! Empty ($ value) {// Save the configuration value cache ('C _ '. $ value, $ _ config) ;}return ;} return null; // avoid invalid parameters}
/*** Process tag extensions, for example, call the project start tag, tag ('app _ in in '); * @ param string $ tag name * @ param mixed $ params input parameter * @ return mixed */function tag ($ tag, & $ params = NULL) {// system or framework label extension // C ('extends ', include THINK_PATH. 'conf/tags. php '); // This file is very important $ extends = C ('extends. '. $ tag); // $ _ config ['extends '] [app_init] // application or project tag extension // C ('tags', include CONF_PATH. 'tags. php '); // You can also compile your own tags in the project. PHP File $ tags = C ('tags. '. $ tag); // $ _ Config ['tags'] [app_init] if (! Empty ($ tags) {// if $ tags is not empty if (empty ($ tags ['_ overlay']) &! Empty ($ extends) {// merged extension $ tags = array_unique (array_merge ($ extends, $ tags ));} elseif (isset ($ tags ['_ overlay']) {// overlay the system tag unset by setting '_ overlay' => 1 ($ tags ['_ overlay']);} elseif (! Empty ($ extends) {$ tags = $ extends;} if ($ tags) {if (APP_DEBUG) {G ($ tag. 'start'); trace ('['. $ tag. '] -- START --', '', 'info');} // execute the extended foreach ($ tags as $ key => $ name) {if (! Is_int ($ key) {// specifies the full path of the behavior class for mode extension $ name = $ key;} // creates the behavior class object, for example: $ class = $ name. 'behavior '; $ Behavior = new $ class (); // creates an action class object B ($ name, $ params); // executes an action, parameter 1-behavior name parameter 2-input parameter} if (APP_DEBUG) {// trace ('[') of the Execution log that records behavior ('['. $ tag. '] -- END -- [RunTime :'. G ($ tag. 'start', $ tag. 'end', 6 ). 's] ', '', 'info') ;}} else {// return false if no action is executed ;}}
/*** Execute a behavior * @ param string $ name behavior name * @ param Mixed $ params parameter * @ return void */function B ($ name, & $ params = NULL) {// behavior class in Think. class. the autoload ($ class) function in the PHP file automatically loads $ class = $ name. 'behavior '; // name of the Behavior class G ('behaviorstart'); $ behavior = new $ class (); // create a behavior class Object $ behavior-> run ($ params); // pass the parameter if (APP_DEBUG) to the current behavior class Object) {// trace ('run '. $ name. 'behavior [RunTime :'. G ('behaviorstart', 'behaviorend', 6 ). 's] ', '', 'info ');}}

Note: The behavior class, model class, and controller class are automatically loaded in the autoload function in the Think. class. php file:

/*** The system automatically loads the ThinkPHP class library * and supports configuring the automatic loading path * @ param string $ class object class name * @ return void * // behavior class, model class, Controller classes are all public static function autoload ($ class) automatically loaded through the autoload function) {// check whether the alias Defines if (alias_import ($ class) return; // The behavior class is automatically loaded through this function if (substr ($ class,-8) = 'behavior ') {// load Behavior if (require_cache (CORE_PATH. 'behavior /'. $ class. '. class. php ') | require_cache (EXTEND_PATH. 'behavior /'. $ class. '. class. php ') | require_cache (LIB_PATH. 'behavior /'. $ class. '. class. php ') | (defined ('mode _ name') & require_cache (MODE_PATH.ucwords (MODE_NAME ). '/Behavior /'. $ class. '. class. php ') {return ;}} elseif (substr ($ class,-5) = 'model ') {// load model if (defined ('group _ name') & require_cache (LIB_PATH. 'model /'. GROUP_NAME. '/'. $ class. '. class. php ') | require_cache (LIB_PATH. 'model /'. $ class. '. class. php ') | require_cache (EXTEND_PATH. 'model /'. $ class. '. class. php ') {return ;}} elseif (substr ($ class,-6) = 'action ') {// load the controller if (defined ('group _ name') & require_cache (LIB_PATH. 'Action /'. GROUP_NAME. '/'. $ class. '. class. php ') | require_cache (LIB_PATH. 'Action /'. $ class. '. class. php ') | require_cache (EXTEND_PATH. 'Action /'. $ class. '. class. php ') {return ;}// Based on the automatic loading path settings, try to search $ paths = explode (', ', C ('app _ AUTOLOAD_PATH ')); foreach ($ paths as $ path) {if (import ($ path. '. '. $ class) // return if the class is successfully loaded ;}}

PS: We recommend several formatting, beautification, and conversion tools on this site to help you sort out messy code. I believe you can use them in future development:

Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat

JavaScript code beautification/compression/formatting/encryption tools:
Http://tools.jb51.net/code/jscompress

Online XML formatting/compression tools:
Http://tools.jb51.net/code/xmlformat

JSON code formatting and beautification tools:
Http://tools.jb51.net/code/json

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Json code online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat

SQL code online formatting and beautification tools:
Http://tools.jb51.net/code/sqlcodeformat

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.