Extend the PHPWord class in CI - class not found

5

I'm trying to put the PHPWord library in codeigniter, so I downloaded PHPWord and extracted the PHPWord folder and the PhpWord.php file to the third_party folder of CI. After that I created in the libraries folder a file with the name word.php that would extend the functionalities of PhpWord.php.

So far so good, but when I call the library Word.php in my controller I get the following error

  

Class PHPWORD not found in (path-of-file / Word.php).

I've heard that before doing this I would need to call the autoloader file, I tried that way too and I did not succeed, anyone know how to help me?

Here is the code for the file Word.PHP

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    require_once APPPATH."/third_party/PHPWord.php"; 

    class Word extends PHPWord { 
        public function __construct() { 
            parent::__construct(); 
        } 
}

I followed a part of this tutorial I found on the internet:

link

    
asked by anonymous 09.01.2015 / 12:00

1 answer

4

The problem you're having is basically calling a file that does not exist.

PHPWord has undergone several changes since the date this tutorial was created (2012). It started to support PSR-0 for autoload and to use Namespaces, which greatly modified (for the better) the project architecture. Unfortunately Codeigniter "stopped" in time and has no support for any of it.

You have two options:

  • Use the autoloader of the latest version and also include the Namespace (requires at least PHP 5.3). If you look at the file paths, if they really exist, etc ...

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
        require_once APPPATH."/third_party/PHPWord/autoload.php";
    
        class Word extends PHPOffice\PHPWord { 
            public function __construct() { 
                parent::__construct(); 
            } 
    }
    
  • Use an older version with the library structure similar to the one in the tutorial you are using (in this case at 0.8.1). The folder structure is the same:

  • link

    PS.:Lookforanupdatewithwhat'snewinPHPbeforeyoustartdeveloping.I'msorrytotellyoubutyou'realreadygeneratingobsoletecodeifyou'reworkingonanewprojectwithCodeigniter.

    Oneofthebestwaystogetstartedis PHP: The Right Way .

        
    09.01.2015 / 13:10