美文网首页
spl_autoload_register

spl_autoload_register

作者: 码氪丝 | 来源:发表于2019-11-30 10:49 被阅读0次

    踩坑

    方法:sql_autoload(class,[ext_name])
    其中$class为传入的类名,由于历史习惯愿意,他会将类名强制转换为小写进行加载,也就是说文件名不能出现大写,假如需要使用spl_autoload方法则类文件名称必须小写了。假如一定要有大写字母的解决办法就是解决spl_autoload方法(即:老子不用它),可以使用__autoload或者incloud_once代替。

    基于spl_autoload_register 的自动装载类

    <?php
    class autoloader {
      public static $loader;
      public static function init() {
        if (self::$loader == NULL)
          self::$loader = new self ();
        return self::$loader;
      }
    
      public function __construct() {
        spl_autoload_register ( array ($this, 'model' ) );
        spl_autoload_register ( array ($this, 'helper' ) );
        spl_autoload_register ( array ($this, 'controller' ) );
        spl_autoload_register ( array ($this, 'library' ) );
      }
    
      public function library($class) {
        set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' );
        spl_autoload_extensions ( '.library.php' );
        spl_autoload ( $class );
      }
      
      public function controller($class) {
        $class = preg_replace ( '/_controller$/ui', '', $class );
        set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' );
        spl_autoload_extensions ( '.controller.php' );
        spl_autoload ( $class );
      }
    
      public function model($class) {
        $class = preg_replace ( '/_model$/ui', '', $class );
        set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' );
        spl_autoload_extensions ( '.model.php' );
        spl_autoload ( $class );
      }
      
      public function helper($class) {
        $class = preg_replace ( '/_helper$/ui', '', $class );
        set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' );
        spl_autoload_extensions ( '.helper.php' );
        spl_autoload ( $class );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:spl_autoload_register

          本文链接:https://www.haomeiwen.com/subject/gqfowctx.html