大家都知道include_once可以一次加载,很方便。但是他的原理确实每次都要去查询是否记载了这个文件,使得效率很差,但是可以使用include来提升效率,但如何才能使用include实现只记载一次呢? 这么我分析了某源码得到:
- /**
- * 加载函数库
- * @param string $func 函数库名
- * @param string $path 地址
- */
- private static function _load_func($func, $path = '') {
- static $funcs = array();
- if (emptyempty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions';
- $path .= DIRECTORY_SEPARATOR.$func.'.func.php';
- $key = md5($path);
- if (isset($funcs[$key])) return true;
- if (file_exists(PC_PATH.$path)) {
- include PC_PATH.$path;
- } else {
- $funcs[$key] = false;
- return false;
- }
- $funcs[$key] = true;
- return true;
- }
不难看出,他通过static全局变量来判断是否加载过了某文件。