CI中引用外部类库报出“ Cannot redeclare class IOFactory”错误

来源:互联网 发布:网络招聘的优劣势 编辑:程序博客网 时间:2024/05/22 03:20

明确知道是重复定义声明了IOFactory,出现这个错误的时候我首先想到的是自己之前引用的外部类库PHPWord中的IOFactory和PHPPowerPoint中的IOFactory冲突了。

先说明下项目是在CI框架下,在application/library下引用phpWord已经实现了导出word,但我在引入PHPPowerPoint时,总是提示重复申明IOfactory。PHPPowerPoint在没引入框架前是可以实现导出ppt的。
我是这样安装PHPWord和PHPPowerPoint的:
1) 解压压缩包里的Classes文件夹中的内容到application\libraries\目录下,目录结构如下:
– application\libraries\PHPPowerPoint.php
– application\libraries\PHPPowerPoint(文件夹)

2)控制器调用语句如下:
this>load>library(PHPPowerPoint);this->load->library(‘PHPPowerPoint/IOfactory’);

到这儿,我的想法是先把PHPPowerPoint/IOfactory重命名为IOfactory_ppt,区分PHPWord/IOfactory,重新运行之后仍然报出重复申明类“ Cannot redeclare class IOfactory_ppt”

那这样就说明并不是和PHPWord/IOfactory重复了,然后重新思考下,认为是我引用PHPPowerPoint类库时,为了避免PHPPowerPoint类库内部资源引用混乱,依葫芦画瓢写了一个Autoloader.php,这个PHPPowerPoint_Autoloader是一个加载其内部样式类等资源的自动加载类,详细代码如下

class PHPPowerPoint_Autoloader{    public static function Register() {        return spl_autoload_register(array('PHPPowerPoint_Autoloader', 'Load'));//与注册的自动装载函数    }    public static function Load($strObjectName) {        if((class_exists($strObjectName)) || (strpos($strObjectName, 'PHPPowerPoint') === false)) {            return false;        }        $strObjectFilePath = PHPPower_BASE_PATH . str_replace('_', '/', $strObjectName) . '.php';        if((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) {            return false;        }        require($strObjectFilePath);    }}

,注意通过这个类,已经是加载了一次IOfactory,然后再在CI下 application\controllers\PowerPointExport.php中为了装载外部类库,使用CI框架下加载类库的方法

$this->load->library('PHPPowerpoint');$this->load->library('PHPPowerpoint/IOFactory');

在这儿,通过$this->load->library(‘PHPPowerpoint/IOFactory’);再一次加载了IOFactory类。

$this->load方法的实现是通过system/core/Loader.php具体实现地

/**     * CI Autoloader     *     * Loads component listed in the config/autoload.php file.     *     * @used-by CI_Loader::initialize()     * @return  void     */    protected function _ci_autoloader()    {        if (file_exists(APPPATH.'config/autoload.php'))        {            include(APPPATH.'config/autoload.php');        }        if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/autoload.php'))        {            include(APPPATH.'config/'.ENVIRONMENT.'/autoload.php');        }        if ( ! isset($autoload))        {            return;        }        // Autoload packages        if (isset($autoload['packages']))        {            foreach ($autoload['packages'] as $package_path)            {                $this->add_package_path($package_path);            }        }        // Load any custom config file        if (count($autoload['config']) > 0)        {            foreach ($autoload['config'] as $val)            {                $this->config($val);            }        }        // Autoload helpers and languages        foreach (array('helper', 'language') as $type)        {            if (isset($autoload[$type]) && count($autoload[$type]) > 0)            {                $this->$type($autoload[$type]);            }        }        // Autoload drivers        if (isset($autoload['drivers']))        {            $this->driver($autoload['drivers']);        }        // Load libraries        if (isset($autoload['libraries']) && count($autoload['libraries']) > 0)        {            // Load the database driver.            if (in_array('database', $autoload['libraries']))            {                $this->database();                $autoload['libraries'] = array_diff($autoload['libraries'], array('database'));            }            // Load all other libraries            $this->library($autoload['libraries']);        }        // Autoload models        if (isset($autoload['model']))        {            $this->model($autoload['model']);        }    }

那么到这儿,报错的根源可能就是自己依葫芦画瓢写的Autoload
与CI中的Autoload 类冲突,这样就不能使用$this->load->library来引用类库了,然后换成

    define('ROOT_PATH',dirname(dirname(__FILE__)));    include_once ROOT_PATH.'/libraries/PHPPowerpoint.php';    include_once ROOT_PATH.'/libraries/PHPPowerpoint/IOFactory.php';

果然能够导出ppt.。问题解决。

感谢CI论坛中Hex为我解答疑惑提供思路。
http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=24254&extra=page%3D1

1 0
原创粉丝点击