如果只是從了解怎么用這幾函數的角度看,直接看官方發布的文檔即可!但是要了解這些函數是怎么被ThinkPHP框架定義調用的,那需要另外一番討 論了。還好,ThinkPHP官方在源碼力做了很好的注釋,通過一步步探索我發現只要你找到代碼的定義位置,你就能更精確的把握其應用,甚至不用讀代碼, 官方的代碼注釋就能幫你很好的理解。廢話不多說,來看我找的框架執行代碼的幾個位置:
首先,從入口文件index.php里面看到框架的執行通過“require './ThinkPHP/ThinkPHP.php';”執行,打開這個文件,看里面的start() 方法:
................................
static public function start() {
// 注冊AUTOLOAD方法
spl_autoload_register('Think\Think::autoload');
// 設定錯誤和異常處理
register_shutdown_function('Think\Think::fatalError');
set_error_handler('Think\Think::appError');
set_exception_handler('Think\Think::appException');
// 初始化文件存儲方式
Storage::connect(STORAGE_TYPE);
$runtimefile = RUNTIME_PATH.APP_MODE.'~runtime.php';
if(!APP_DEBUG && Storage::has($runtimefile)){
Storage::load($runtimefile);
}else{
if(Storage::has($runtimefile))
Storage::unlink($runtimefile);
$content = '';
// 讀取應用模式
$mode = include is_file(CONF_PATH.'core.php')?CONF_PATH.'core.php':MODE_PATH.APP_MODE.'.php';
// 加載核心文件
/*Array
(
[0] => C:\wamp\www\onthink\ThinkPHP/Common/functions.php
[1] => ./Application/Common/Common/function.php
[2] => C:\wamp\www\onthink\ThinkPHP\Library/Think/Hook.class.php
[3] => C:\wamp\www\onthink\ThinkPHP\Library/Think/App.class.php
[4] => C:\wamp\www\onthink\ThinkPHP\Library/Think/Dispatcher.class.php
[5] => C:\wamp\www\onthink\ThinkPHP\Library/Think/Route.class.php
[6] => C:\wamp\www\onthink\ThinkPHP\Library/Think/Controller.class.php
[7] => C:\wamp\www\onthink\ThinkPHP\Library/Think/View.class.php
[8] => C:\wamp\www\onthink\ThinkPHP\Library/Behavior/ParseTemplateBehavior.class.php
[9] => C:\wamp\www\onthink\ThinkPHP\Library/Behavior/ContentReplaceBehavior.class.php
)
*/
foreach ($mode['core'] as $file){
if(is_file($file)) {
include $file;
if(!APP_DEBUG) $content .= compile($file);
}
}
// 加載應用模式配置文件
foreach ($mode['config'] as $key=>$file){
is_numeric($key)?C(include $file):C($key,include $file);
}
// 讀取當前應用模式對應的配置文件
if('common' != APP_MODE && is_file(CONF_PATH.'config_'.APP_MODE.'.php'))
C(include CONF_PATH.'config_'.APP_MODE.'.php');
// 加載模式別名定義
if(isset($mode['alias'])){
self::addMap(is_array($mode['alias'])?$mode['alias']:include $mode['alias']);
}
// 加載應用別名定義文件
if(is_file(CONF_PATH.'alias.php'))
self::addMap(include CONF_PATH.'alias.php');
// 加載模式行為定義
if(isset($mode['tags'])) {
Hook::import(is_array($mode['tags'])?$mode['tags']:include $mode['tags']);
}
// 加載應用行為定義
if(is_file(CONF_PATH.'tags.php'))
// 允許應用增加開發模式配置定義
Hook::import(include CONF_PATH.'tags.php');
// 加載框架底層語言包
L(include THINK_PATH.'Lang/'.strtolower(C('DEFAULT_LANG')).'.php');
if(!APP_DEBUG){
$content .= "\nnamespace { Think\Think::addMap(".var_export(self::$_map,true).");";
$content .= "\nL(".var_export(L(),true).");\nC(".var_export(C(),true).');Think\Hook::import('.var_export(Hook::get(),true).');}';
Storage::put($runtimefile,strip_whitespace('<?php '.$content));
}else{
// 調試模式加載系統默認的配置文件
C(include THINK_PATH.'Conf/debug.php');
// 讀取應用調試配置文件
if(is_file(CONF_PATH.'debug.php'))
C(include CONF_PATH.'debug.php');
}
}
上 面那個打印出來的數組是我加上去的,可以看出來,這個數組就是ThinkPHP加載的核心文件,其中包括 “ThinkPHP/Common/functions.php”和“ ./Application/Common/Common/function.php”;在前面的functions.php文件里面會發現各種函數庫:
/**
* 獲取和設置配置參數 支持批量定義
* @param string|array $name 配置變量
* @param mixed $value 配置值
* @param mixed $default 默認值
* @return mixed
*/
function C($name=null, $value=null,$default=null) {
static $_config = array();
// 無參數時獲取所有
if (empty($name)) {
return $_config;
}
// 優先執行設置獲取或賦值
if (is_string($name)) {
if (!strpos($name, '.')) {
$name = strtolower($name);
if (is_null($value))
return isset($_config[$name]) ? $_config[$name] : $default;
$_config[$name] = $value;
return;
}
// 二維數組設置和獲取支持
$name = explode('.', $name);
$name[0] = strtolower($name[0]);
if (is_null($value))
return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : $default;
$_config[$name[0]][$name[1]] = $value;
return;
}
// 批量設置
if (is_array($name)){
$_config = array_merge($_config, array_change_key_case($name));
return;
}
return null; // 避免非法參數
}
/**
* 拋出異常處理
* @param string $msg 異常消息
* @param integer $code 異常代碼 默認為0
* @return void
*/
function E($msg, $code=0) {
throw new Think\Exception($msg, $code);
}
/**
* 記錄和統計時間(微秒)和內存使用情況
* 使用方法:
* <code>
* G('begin'); // 記錄開始標記位
* // ... 區間運行代碼
* G('end'); // 記錄結束標簽位
* echo G('begin','end',6); // 統計區間運行時間 精確到小數后6位
* echo G('begin','end','m'); // 統計區間內存使用情況
* 如果end標記位沒有定義,則會自動以當前作為標記位
* 其中統計內存使用需要 MEMORY_LIMIT_ON 常量為true才有效
* </code>
* @param string $start 開始標簽
* @param string $end 結束標簽
* @param integer|string $dec 小數位或者m
* @return mixed
*/
function G($start,$end='',$dec=4) {
static $_info = array();
static $_mem = array();
if(is_float($end)) { // 記錄時間
$_info[$start] = $end;
}elseif(!empty($end)){ // 統計時間和內存使用
if(!isset($_info[$end])) $_info[$end] = microtime(TRUE);
if(MEMORY_LIMIT_ON && $dec=='m'){
if(!isset($_mem[$end])) $_mem[$end] = memory_get_usage();
return number_format(($_mem[$end]-$_mem[$start])/1024);
}else{
return number_format(($_info[$end]-$_info[$start]),$dec);
}
}else{ // 記錄時間和內存使用
$_info[$start] = microtime(TRUE);
if(MEMORY_LIMIT_ON) $_mem[$start] = memory_get_usage();
}
}
當然,C、E、G、L、T、I、N...等函數也在其中,呵呵 是不是發現不止這些函數的定義,這里的資源只要了解就能精確把握應用;至於每個函數的用法不在多說......
制作人:飛虎 無兄弟不編程!
=====================================================================================
歡迎加QQ群進行更多交流:305397511 專注於php、mysql、jquery以及開源框架