首页 > 语言 > PHP > 正文

PHP类的自动载入程序代码

2024-09-04 11:43:49
字体:大 中 小
来源:转载
供稿:网友

自动载入类在php中魔术方法了,我们可以通过php中的_autoload方法来实现了,下面一起来看一篇关于PHP类的自动载入程序代码,希望本文章对各位有帮助.

加入我们现在有两个PHP文件,内容如下:

Test1.php

  1. <?php 
  2.  
  3. class Test1 { 
  4.  
  5.     public function func1() { 
  6.  
  7.         return 'test1'; 
  8.  
  9.     } 
  10.  
  11. } 
  12.  
  13. ?> 

Test2.php

  1. <?php 
  2.  
  3. class Test2 { 
  4.  
  5.     public function func2() { 
  6.  
  7.         return 'test2'; 
  8.  
  9.     } 
  10.  
  11. } 
  12.  
  13. ?> 

然而在需要载入这两个文件时,传统的写法是这样的:

  1. <?php 
  2. require ('Test1.php'); 
  3. require ('Test2.php'); 
  4. $TestObj1 = new Test1(); 
  5. $TestObj2 = new Test2(); 
  6. echo $TestObj1->func1().'<br/>'; 
  7. echo $TestObj2->func2(); 
  8. ?> 

现在我们使用PHP类的自动载入,只需要定义 __autoload() 方法既可将类自动载入,方法如下:

  1. <?php 
  2. //define autoload function 
  3. function __autoload($class) { 
  4.     require __DIR__.'/'.$class.'.php'; 
  5. } //Vevb.com 
  6. $TestObj1 = new Test1(); 
  7. $TestObj2 = new Test2(); 
  8. echo $TestObj1->func1().'<br/>'; 
  9. echo $TestObj2->func2(); 
  10. ?> 

很方便吧,可是之后__autoload这个函数被废弃掉了,主要原因是因为,我们一个PHP的项目可能会依赖多个框架,如果我们每一个框架都拥有这个函数,那么程序就会报一个函数重复定义的致命错误,当然不用担心,在PHP5.3之后呢,官方提供了一个 spl_autoload_register() 函数来取代 __autoload,这个函数的特点是它允许你存在多个相同的载入函数,即使我写了多个载入,也不会出现任何问题,代码如下:

  1. <?php 
  2. spl_autoload_register(autoload1); 
  3. spl_autoload_register(autoload2); 
  4. //define autoload function 
  5. function autoload1($class) { 
  6.     require __DIR__.'/'.$class.'.php'; 
  7. } 
  8. function autoload2($class) { 
  9.     require __DIR__.'/'.$class.'.php'; 
  10. } 
  11. $TestObj1 = new Test1(); 
  12. $TestObj2 = new Test2(); 
  13. echo $TestObj1->func1().'<br/>'; 
  14. echo $TestObj2->func2(); 
  15. ?> 

这种方法会更先进一些,也是我们采用的最主要的方法。博主最近开发的项目使用的是ThinkPHP框架,就在框架的核心文件Think.class.php中找到它的自动载入函数,拷过来给大家看下。

  1. static public function start() { 
  2.  
  3.     // 注册AUTOLOAD方法 
  4.  
  5.     spl_autoload_register('Think/Think::autoload'); 
  6.  
  7. } 
  8.  
  9. /** 
  10.  
  11.  * 类库自动加载 
  12.  
  13.  * @param string $class 对象类名 
  14.  
  15.  * @return void 
  16.  
  17.  */ 
  18.  
  19. public static function autoload($class) { 
  20.  
  21.     // 检查是否存在映射 
  22.  
  23.     if(isset(self::$_map[$class])) { 
  24.  
  25.         <a href="/tags.php/include/" target="_blank">include</a> self::$_map[$class]; 
  26.  
  27.     } elseif (false !== strpos($class,'//')){ 
  28.  
  29.         $name = strstr($class, '//', true); 
  30.  
  31.         if(in_array($name,array('Think','Org','Behavior','Com','Vendor')) || is_dir(LIB_PATH.$name)){ 
  32.  
  33.         // Library目录下面的命名空间自动定位 
  34.  
  35.         $path = LIB_PATH; 
  36.  
  37.         }else{ 
  38.  
  39.             // 检测自定义命名空间 否则就以模块为命名空间 
  40.  
  41.             $namespace  =   C('AUTOLOAD_NAMESPACE'); 
  42.  
  43.             $path = isset($namespace[$name])? dirname($namespace[$name]).'/' : APP_PATH; 
  44.  
  45.         } 
  46.  
  47.         $filename = $path . str_replace('//', '/', $class) . EXT; 
  48.  
  49.         if(is_file($filename)) { 
  50.  
  51.             // Win环境下面严格区分大小写 
  52.  
  53.             if (IS_WIN && false === strpos(str_replace('/', '//', realpath($filename)), $class . EXT)){ 
  54.  
  55.                 return ; 
  56.  
  57.             } 
  58.  
  59.             include $filename; 
  60.  
  61.         } 
  62.  
  63.     }elseif (!C('APP_USE_NAMESPACE')) { 
  64.  
  65.         // 自动加载的类库层 
  66.  
  67.         <a href="/tags.php/foreach/" target="_blank">foreach</a>(<a href="/tags.php/explode/" target="_blank">explode</a>(',',C('APP_AUTOLOAD_LAYER')) as $layer){ 
  68.  
  69.             if(<a href="/tags.php/substr/" target="_blank">substr</a>($class,-strlen($layer))==$layer){ 
  70.  
  71.                 if(require_cache(MODULE_PATH.$layer.'/'.$class.EXT)) { 
  72.  
  73.                     return ; 
  74.  
  75.                 } 
  76.  
  77.             } 
  78.  
  79.         } 
  80.  
  81.         // 根据自动加载路径设置进行尝试搜索 
  82.  
  83.         foreach (explode(',',C('APP_AUTOLOAD_PATH')) as $path){ 
  84.  
  85.             if(import($path.'.'.$class)) 
  86.  
  87.                 // 如果加载类成功则返回 
  88.  
  89.                 return ; 
  90.  
  91.         } 
  92.  
  93.     } 
  94.  
  95. } 

更多的相关知识大家可以自行去搜索,或者查看相关手册.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表