当前位置:Gxlcms > PHP教程 > php类自动加载

php类自动加载

时间:2021-07-01 10:21:17 帮助过:40人阅读

rare内置了类自动装载功能,当使用一个类的使用直接使用而无须require(include) 类文件。

该类自动装载功能非常的独立,若你需要,可以直接在其他框架(任意php程序)中使用。

1.先引入 rareAutoLoad.class.php

2.注册功能

  1. /**
  2. * 类自动装载
  3. * http://raremvc.googlecode.com
  4. * http://rare.hongtao3.com
  5. * @example
  6. * include 'rareAutoLoad.php';
  7. * $option=array('dirs'=>'/www/lib/share/,/www/lib/api/',//class 从那些目录中查找
  8. * 'cache'=>'/tmp/111111.php',//class path 缓存文件
  9. * ‘suffix’=>'.class.php' //需要类自动装载的php类文件的后缀
  10. * "hand"=>true, //是否手动更新class 路径文件 ,为false 时 缓存文件写入到指定的cache文件中去,
  11. * //为true 是需要手动允许 autoLoad.php 文件
  12. * );
  13. * rareAutoLoad::register($option);
  14. *
  15. * 参考了symfony 的类自动装载
  16. * 为了提供效率,将类的位置保存到缓存文件中,在第一次使用的时候会对dirs中的文件目录进行扫描
  17. * 需要自动装载的类的文件命名 要求 必须 以 .class.php 结束,如文件名为 a.class.php 中定义的类可以被扫描到而 a.php的文件会忽略掉
  18. * 类名 和 文件命名 可以没有关系 如 a.class.php 文件中 可以定义 class b{}
  19. *
  20. * @author duwei
  21. *
  22. */
  23. class rareAutoLoad
  24. {
  25. private static $instance=null;
  26. private static $registered=false;
  27. private $cacheFile=null;
  28. private $classes=array();//对应class 类名 和对应文件路径
  29. private $option;
  30. private $hand=false;//是否手动运行该脚本进行class路径扫描,
  31. private $reloadCount=0;//reload操作的次数
  32. /**
  33. * @param array $option 需要参数 dirs:扫描目录 cache:缓存文件
  34. */
  35. public function __construct($option){
  36. if(!isset($option['suffix'])) $option['suffix']=".class.php";//文件后缀
  37. $this->option=$option;
  38. if(!isset($option['cache'])){
  39. $trac=debug_backtrace(false);
  40. $calFile=$trac[2]['file'];
  41. $option['cache']="/tmp/rareautoLoad_".md5($calFile)."_".filemtime($calFile);
  42. @unlink($option['cache']);
  43. }
  44. if(isset($option['hand']))$this->hand=(boolean)$option['hand'];
  45. $this->cacheFile=$option['cache'].".php";
  46. $this->getClasses();
  47. }
  48. /**
  49. * 获取DAutoLoad 的单实例对象
  50. * @param array $option
  51. * @return DAutoLoad
  52. */
  53. private static function getInstance($option){
  54. if (!isset(self::$instance)){
  55. self::$instance = new rareAutoLoad($option);
  56. }
  57. return self::$instance;
  58. }
  59. /**
  60. * 注册自动装载
  61. * @param array $option array('dirs'=>'/www/lib/share/,/www/lib/api/','cache'=>'/tmp/111111.php');
  62. * @throws Exception
  63. */
  64. public static function register($option) {
  65. if (self::$registered)return;
  66. // ini_set('unserialize_callback_func', 'spl_autoload_call');
  67. if (false === spl_autoload_register(array(self::getInstance($option), 'autoload'))){
  68. die(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
  69. }
  70. self::$registered = true;
  71. }
  72. /**
  73. * spl_autoload_call 调用 load class
  74. * 若缓存文件中的类的路径不正确,会尝试reload一次
  75. * 对reload后还不存在的类 缓存中记录其key,标记为 false,以避免缓存文件多次无效的更新
  76. * 对于使用 class_exists 进行判断时默认会进行autoload操作
  77. * @param $class
  78. * @return
  79. */
  80. public function autoload($class){
  81. if(class_exists($class, false) || interface_exists($class, false)) return true;
  82. if ($this->classes && isset($this->classes[$class]) ){
  83. $file=$this->classes[$class];
  84. if(!$file)return false;
  85. if(!file_exists($file) && !$this->hand){
  86. $this->reload();
  87. return $this->autoload($class);
  88. }
  89. require($file);
  90. return true;
  91. }{
  92. $this->reload();
  93. if(isset($this->classes[$class])){
  94. $file=$this->classes[$class];
  95. if(!$file)return false;
  96. require($file);
  97. return true;
  98. }else{
  99. $this->classes[$class]=false;
  100. $this->saveCache();
  101. }
  102. }
  103. return false;
  104. }
  105. /**
  106. * 获取类名列表
  107. * @return
  108. */
  109. private function getClasses(){
  110. if(file_exists($this->cacheFile)){
  111. $this->classes=require($this->cacheFile);
  112. if(is_array($this->classes))return true;
  113. }
  114. $this->classes=array();
  115. $this->reload();
  116. }
  117. /**
  118. * 重新扫描一次
  119. * 并将类名的位置信息保存到cache 中
  120. * @return
  121. */
  122. private function reload(){
  123. $this->reloadCount++;
  124. if($this->hand)return;
  125. $cachedir=dirname($this->cacheFile);
  126. $this->directory($cachedir);
  127. if(!is_writable($cachedir)) die('can not write cache!');
  128. settype($this->classes, 'array');
  129. $dirs=$this->option['dirs'];
  130. if(!is_array($dirs)) $dirs=explode(",", $dirs);
  131. $dirs=array_unique($dirs);
  132. foreach($dirs as $dir){
  133. if(!$dir || !file_exists($dir))continue;
  134. $this->scanDir($dir);
  135. }
  136. $this->saveCache();
  137. }
  138. private function saveCache(){
  139. if($this->hand)return;
  140. $phpData=" if(!is_array($this->classes))$this->classes=array();
  141. ksort($this->classes);
  142. $phpData.="return ".var_export($this->classes,true).";";
  143. file_put_contents($this->cacheFile, $phpData,LOCK_EX);
  144. clearstatcache();
  145. }
  146. /**
  147. * 扫描文件夹以及文件
  148. * 只有 $this->option['suffix'] 命名的文件才会被扫描到
  149. * @param $dir
  150. * @return
  151. */
  152. private function scanDir($dir){
  153. $files=scandir($dir,1);
  154. foreach($files as $fileName){
  155. $file=rtrim($dir,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$fileName;
  156. if(is_dir($file) && strpos($fileName,'.')!==0){
  157. $this->scanDir($file);
  158. }else{
  159. if($this->str_endWith($fileName,$this->option['suffix'])){
  160. preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
  161. foreach ($classes[1] as $class){
  162. $this->classes[$class] = $file;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. private function directory($dir){
  169. return is_dir($dir) or ($this->directory(dirname($dir)) and mkdir($dir, 0777));
  170. }
  171. function str_endWith($str,$subStr){
  172. return substr($str, -(strlen($subStr)))==$subStr;
  173. }
  174. }

人气教程排行