明確知道是重複定義聲明瞭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)控制器調用語句如下:
到這兒,我的想法是先把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