大家都知道include_once可以一次加载,很方便。但是他的原理确实每次都要去查询是否记载了这个文件,使得效率很差,但是可以使用include来提升效率,但如何才能使用include实现只记载一次呢? 这么我分析了某源码得到:

 
  
  1. /** 
  2.      * 加载函数库 
  3.      * @param string $func 函数库名 
  4.      * @param string $path 地址 
  5.      */ 
  6.     private static function _load_func($func$path = '') { 
  7.         static $funcs = array(); 
  8.         if (emptyempty($path)) $path = 'libs'.DIRECTORY_SEPARATOR.'functions'
  9.         $path .= DIRECTORY_SEPARATOR.$func.'.func.php'
  10.         $key = md5($path); 
  11.         if (isset($funcs[$key])) return true; 
  12.         if (file_exists(PC_PATH.$path)) { 
  13.             include PC_PATH.$path
  14.         } else { 
  15.             $funcs[$key] = false; 
  16.             return false; 
  17.         } 
  18.         $funcs[$key] = true; 
  19.         return true; 
  20.     } 

不难看出,他通过static全局变量来判断是否加载过了某文件。