phpexcel引入MVC框架会导致__autoload引入类文件失败的解决办法

Autoloader.php 的register和load方法

register方法

  if (function_exists('__autoload')) {
            //    Register any existing autoloader function with SPL, so we don't get any clashes
            spl_autoload_register('__autoload');
        }
        //    Register ourselves with SPL
        return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));

 

load方法

 if ((class_exists($pClassName,FALSE)) || (strpos($pClassName, 'PHPExcel') !== 0)) {
            //    Either already loaded, or not a PHPExcel class request
            return FALSE;
        }

        $pClassFilePath = PHPEXCEL_ROOT .
                          str_replace('_',DIRECTORY_SEPARATOR,$pClassName) .
                          '.php';

        if ((file_exists($pClassFilePath) === FALSE) || (is_readable($pClassFilePath) === FALSE)) {
            //    Can't load
            return FALSE;
        }

        require($pClassFilePath);

 

spl_autoload_register这个方法会去自动注册方法,但是,PHPExcel自身有判断引入类的相对路径,和mvc的相对路径,就会因为相对路径的计算会出错,

spl_autoload_register('__autoload');注册 __autoload 方法因为相对路径的问题,如果你不嫌烦可以挨个处理路径的问题依然可以使用,但是也有简单办法,去吧register方法的经过的所有phpexcel方法全部注册一遍

,不管没有没先把可能在其他地方引入的或者注册的phpEXcel类全部spl_autoload_unregister一下,在应用的时候再去spl_autoload_register,

spl_autoload_register的作用是吧函数到注册到函数队列之中,
吧register 改成下面这样就可以在框架里面使用了
            $functions = spl_autoload_functions();
            foreach ( $functions as  $function){
                spl_autoload_unregister($function);
            }
            $functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions);
            foreach ( $functions as $function){
                $x = spl_autoload_register($function);
            }
            return $x;

  

posted on 2016-06-24 11:35  zh7314  阅读(951)  评论(0编辑  收藏  举报