CI中引用外部类库报出“ Cannot redeclare class IOFactory”错误
2017-04-17 21:01
441 查看
明确知道是重复定义声明了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是一个加载其内部样式类等资源的自动加载类,详细代码如下
,注意通过这个类,已经是加载了一次IOfactory,然后再在CI下 application\controllers\PowerPointExport.php中为了装载外部类库,使用CI框架下加载类库的方法
在这儿,通过$this->load->library(‘PHPPowerpoint/IOFactory’);再一次加载了IOFactory类。
$this->load方法的实现是通过system/core/Loader.php具体实现地
那么到这儿,报错的根源可能就是自己依葫芦画瓢写的Autoload
与CI中的Autoload 类冲突,这样就不能使用$this->load->library来引用类库了,然后换成
果然能够导出ppt.。问题解决。
感谢CI论坛中Hex为我解答疑惑提供思路。
http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=24254&extra=page%3D1
先说明下项目是在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
相关文章推荐
- PHP错误Cannot use object of type stdClass as array in错误的解决办法
- php错误 Fatal error: Cannot use object of type stdClass as array in……
- **PHP错误Cannot use object of type stdClass as array in错误的
- PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function
- Cannot create JDBC driver of class '' for connect URL 'null' 错误
- cocoa编程错误问题:cannot decode object of class (XXX)
- Java编译错误:Cannot reduce the visibility of the inherited method from <class>
- Forward declaration of class cannot have a nested name specifier xcode 升级后cocos2d编译错误
- PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function
- class view中的一个方法实现时出现下面错误 cannot find the definition (implementation) of this function
- tomcat 5.5 Cannot create JDBC driver of class '' for connect URL 'null' 配置数据源错误解决办法
- A project with an Output Type of Class Library cannot be started directly
- 使用TOMCAT5.5连接池连接mysql(解决Cannot create JDBC driver of class '' for connect URL 'null')
- Cannot create JDBC driver of class '' for connect URL 'null'
- spring+hibernate整合时tomcat启动的一个错误(严重: Exception sending context initialized event to listener instance of class org.springframewo
- java反射错误:object is not an instance of declaring class
- 类库引用时的版本错误
- hibernate:newclass.hbm.xml文件错误(the content of element type "class" must match "(meta*,column*,type?generator?...)
- VB6:关于“Only public user defined types defined in public object modules can be used as parameters or return types for public procedures of class modules or as fields of public user defined types.”的编译错误
- 错误 error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用