在翻看TP源碼時發現一個C函數,編者對其做了注釋,函數定義如下:
// 獲取配置值 function C($name=null, $value=null) { //此方法是一個變量庫,static變量每次都會保存下來 static $_config = array(); // 無參數時獲取所有 if (empty($name)) return $_config; // 優先執行設置獲取或賦值 if (is_string($name)) { if (!strpos($name, '.')) { $name = strtolower($name); if (is_null($value)) //若$value爲不空表明是變量注冊,爲空表明是變量查找 return isset($_config[$name]) ? $_config[$name] : null; $_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]] : null; $_config[$name[0]][$name[1]] = $value; return; } // 批量設置 if (is_array($name)) //若$name爲數組,則返回所有的配置變量 return $_config = array_merge($_config, array_change_key_case($name)); return null; // 避免非法參數 }
注意$_config變量前有static修飾,所以$_config在内存中存在於靜態數據段,只要TP程序還在運行,其值將一直存在。C()函數有兩種用法,若只傳一個參數,表示查找常量。若傳兩個參數,表示常量注冊。
還有一點需要說明的是include()的返回值是什麽呢?
include()的返回值一般情況下要包含的文档存在則返回true,不存在則返回false。但是,當要包含的文档中有return的時候include()的返回值是所return的值而不是true OR false。