用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2024|回复: 4
收起左侧

[控制器] 引入类库资源PHPPowerPoint报出“ Cannot redeclare class IOFactory”

[复制链接]
发表于 2017-4-9 21:57:37 | 显示全部楼层 |阅读模式
在application/library下引用phpWord已经实现了导出word,但我在引入PHPPowerPoint时,总是提示重复申明IOfactory。PHPPowerPoint在没引入框架前是可以实现导出ppt的。
我是这样安装PHPWord和PHPPowerPoint的:
1) 解压压缩包里的Classes文件夹中的内容到application\libraries\目录下,目录结构如下:
-- application\libraries\PHPExcel.php
-- application\libraries\PHPExcel (文件夹)

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

3)已经尝试过修改application\libraries\PHPPowerPoint\IOFactory.php 文件
--将其文件名改为Iofactory.php
-- 将其类名从PHPPowerPoint_IOFactory改为Iofactory。
-- 将其构造函数改为public
发表于 2017-4-9 22:40:36 | 显示全部楼层
报错行代码贴一下看看。另外,建议用 include 的方式引入外部类库,特别是这种复杂类库。
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-4-10 10:43:23 | 显示全部楼层
Hex 发表于 2017-4-9 22:40
报错行代码贴一下看看。另外,建议用 include 的方式引入外部类库,特别是这种复杂类库。 ...
  1. A PHP Error was encountered

  2. Severity: Compile Error

  3. Message: Cannot redeclare class IOFactory

  4. Filename: PHPPowerpoint/IOFactory.php

  5. Line Number: 47

  6. Backtrace:
复制代码

/*****************报错代码*******************/
  1. /** PHPPowerPoint */
  2. require_once PHPPower_BASE_PATH.'PHPPowerPoint.php';

  3. /** PHPPowerPoint_IWriter */
  4. require_once PHPPower_BASE_PATH.'PHPPowerPoint/Writer/IWriter.php';

  5. /** PHPPowerPoint_IReader */
  6. require_once PHPPower_BASE_PATH.'PHPPowerPoint/Reader/IReader.php';


  7. /**
  8. * PHPPowerPoint_IOFactory
  9. *
  10. * @category   PHPPowerPoint
  11. * @package    PHPPowerPoint
  12. * @copyright  Copyright (c) 2009 - 2010 PHPPowerPoint (http://www.codeplex.com/PHPPowerPoint)
  13. */
  14. class IOFactory
  15. {        //报错行在这个位置----47行
  16.         /**
  17.          * Search locations
  18.          *
  19.          * @var array
  20.          */
  21.         private static $_searchLocations = array(
  22.                 array( 'type' => 'IWriter', 'path' => 'PHPPowerPoint/Writer/{0}.php', 'class' => 'PHPPowerPoint_Writer_{0}' ),
  23.                 array( 'type' => 'IReader', 'path' => 'PHPPowerPoint/Reader/{0}.php', 'class' => 'PHPPowerPoint_Reader_{0}' )
  24.         );
  25.        
  26.         /**
  27.          * Autoresolve classes
  28.          *
  29.          * @var array
  30.          */
  31.         private static $_autoResolveClasses = array(
  32.                 'Serialized'
  33.         );
  34.        
  35.     /**
  36.      * Private constructor for PHPPowerPoint_IOFactory
  37.      */
  38.     //public function __construct() { }
  39.    
  40.     /**
  41.      * Get search locations
  42.      *
  43.      * @return array
  44.      */
  45.         public static function getSearchLocations() {
  46.                 return self::$_searchLocations;
  47.         }
  48.        
  49.         /**
  50.          * Set search locations
  51.          *
  52.          * @param array $value
  53.          * @throws Exception
  54.          */
  55.         public static function setSearchLocations($value) {
  56.                 if (is_array($value)) {
  57.                         self::$_searchLocations = $value;
  58.                 } else {
  59.                         throw new Exception('Invalid parameter passed.');
  60.                 }
  61.         }
  62.        
  63.         /**
  64.          * Add search location
  65.          *
  66.          * @param string $type                        Example: IWriter
  67.          * @param string $location                Example: PHPPowerPoint/Writer/{0}.php
  68.          * @param string $classname         Example: PHPPowerPoint_Writer_{0}
  69.          */
  70.         public static function addSearchLocation($type = '', $location = '', $classname = '') {
  71.                 self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
  72.         }
  73.        
  74.         /**
  75.          * Create PHPPowerPoint_Writer_IWriter
  76.          *
  77.          * @param PHPPowerPoint $PHPPowerPoint
  78.          * @param string  $writerType        Example: PowerPoint2007
  79.          * @return PHPPowerPoint_Writer_IWriter
  80.          */
  81.         public static function createWriter(PHPPowerPoint $PHPPowerPoint, $writerType = '') {
  82.                 // Search type
  83.                 $searchType = 'IWriter';
  84.                
  85.                 // Include class
  86.                 foreach (self::$_searchLocations as $searchLocation) {
  87.                         if ($searchLocation['type'] == $searchType) {
  88.                                 $className = str_replace('{0}', $writerType, $searchLocation['class']);
  89.                                 $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
  90.                                
  91.                                 if (!class_exists($className)) {
  92.                                         require_once($classFile);
  93.                                 }
  94.                                
  95.                                 $instance = new $className($PHPPowerPoint);
  96.                                 if (!is_null($instance)) {
  97.                                         return $instance;
  98.                                 }
  99.                         }
  100.                 }
  101.                
  102.                 // Nothing found...
  103.                 throw new Exception("No $searchType found for type $writerType");
  104.         }
  105.        
  106.         /**
  107.          * Create PHPPowerPoint_Reader_IReader
  108.          *
  109.          * @param string $readerType        Example: PowerPoint2007
  110.          * @return PHPPowerPoint_Reader_IReader
  111.          */
  112.         public static function createReader($readerType = '') {
  113.                 // Search type
  114.                 $searchType = 'IReader';
  115.                
  116.                 // Include class
  117.                 foreach (self::$_searchLocations as $searchLocation) {
  118.                         if ($searchLocation['type'] == $searchType) {
  119.                                 $className = str_replace('{0}', $readerType, $searchLocation['class']);
  120.                                 $classFile = str_replace('{0}', $readerType, $searchLocation['path']);
  121.                                
  122.                                 if (!class_exists($className)) {
  123.                                         require_once($classFile);
  124.                                 }
  125.                                
  126.                                 $instance = new $className();
  127.                                 if (!is_null($instance)) {
  128.                                         return $instance;
  129.                                 }
  130.                         }
  131.                 }
  132.                
  133.                 // Nothing found...
  134.                 throw new Exception("No $searchType found for type $readerType");
  135.         }
  136.        
  137.         /**
  138.          * Loads PHPPowerPoint from file using automatic PHPPowerPoint_Reader_IReader resolution
  139.          *
  140.          * @param         string                 $pFileName
  141.          * @return        PHPPowerPoint
  142.          * @throws         Exception
  143.          */       
  144.         public static function load($pFilename) {
  145.                 // Try loading using self::$_autoResolveClasses
  146.                 foreach (self::$_autoResolveClasses as $autoResolveClass) {
  147.                         $reader = self::createReader($autoResolveClass);
  148.                         if ($reader->canRead($pFilename)) {
  149.                                 return $reader->load($pFilename);
  150.                         }
  151.                 }
  152.                
  153.                 throw new Exception("Could not automatically determine PHPPowerPoint_Reader_IReader for file.");
  154.         }
  155. }
复制代码
发表于 2017-4-10 11:01:11 | 显示全部楼层
我看了一下文档,建议你用
PHP复制代码
include_once '/path/to/Classes/PHPPowerPoint.php';
$phpPowerPoint = new PHPPowerPoint();
复制代码

方式引入,因为他有自己的 autoloader,应该是和 CI 有冲突。
只要 include 路径搞对,肯定可以用。
 楼主| 发表于 2017-4-10 11:09:10 | 显示全部楼层
Hex 发表于 2017-4-10 11:01
我看了一下文档,建议你用

方式引入,因为他有自己的 autoloader,应该是和 CI 有冲突。

谢谢猪大,我按照你说的方式试一试

本版积分规则