VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php 类自动载入的方法

在PHP5之前,各个PHP框架如果要实现类的自动加载,一般都是按照某种约定自己实现一个遍历目录,自动加载所有符合约定规则的文件的类或函数,当然,PHP5之前对面向对象的支持并不是太好,类的使用也没有现在频繁,我们来详细探讨下吧。

php 类自动载入方法

  1. <?php 
  2. class inload  
  3. { 
  4. /** 
  5.    * 类自动载入,不需要由开发者调用 
  6.    * 
  7.    * @param string $class 类文件 
  8.    */ 
  9.   private function autoload( $class ) 
  10.   { 
  11.     if( emptyempty($class) ) 
  12.     { 
  13.       throw new QException('加载文件不存在'.$class); 
  14.     } 
  15.     else 
  16.     {   
  17.       require _SPRING_.'/_Core/SpringMap.php'; //框架地图 
  18.       if(! file_exists( $source[$class]['file'] ) ) 
  19.       { 
  20.         throw new QException('加载文件不存在'.$class); 
  21.       }  
  22.       require $source[$class]['file']; 
  23.     } 
  24.   } 
  25.      
  26.   /** 
  27.    * 注册或取消注册一个自动类载入方法 
  28.    * 
  29.    * 该方法参考 Zend Framework 
  30.    * 
  31.    * @param string $class 提供自动载入服务的类 
  32.    * @param boolean $enabled 启用或禁用该服务 
  33.    */ 
  34.   private function registerAutoload($class = 'Interpreter' , $enabled = true) 
  35.   { 
  36.     if (!function_exists('spl_autoload_register')) 
  37.     { 
  38.       throw new QException('spl_autoload 不存在这个PHP的安装'); 
  39.     } 
  40.     if ($enabled === true) 
  41.     { 
  42.       spl_autoload_register(array($class, 'autoload')); 
  43.     } 
  44.     else 
  45.     { 
  46.       spl_autoload_unregister(array($class, 'autoload')); 
  47.     } 
  48.   } 
  49.      
  50.   /** 
  51.   * 析构函数 
  52.   */ 
  53.   public function __destruct() 
  54.   { 
  55.     self::registerAutoload('Interpreter' , false); 
  56.   } 

以上所述就是本文的全部内容了,希望大家能够喜欢。

 

出处:http://www.phpfensi.com/php/20210527/15942.html


相关教程