Extend Phpword class in CI - class not found

Asked

Viewed 442 times

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 into the third_party folder of CI. After this I created in the folder Libraries a file with the name word.php which would extend the functionality 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 (filepath/Word.php).

I have heard that before doing this I would need to call the autoloader file, I tried that way too and did not succeed, someone knowing help me ?

Follow the file code 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 in this tutorial I found on the internet :

http://www.ahowto.net/php/creating-ms-word-document-using-codeigniter-and-phpword/

  • 1

    What version of phpword are you using ?

  • I am using version 0.12.0 stable. got it from github : https://github.com/PHPOffice/PHPWord

1 answer

4


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

Phpword has undergone several changes since the date this tutorial was created (2012). He started supporting PSR-0 for autoload and using Namespaces, which greatly (for the better) modified the design architecture. Unfortunately Codeigniter "stopped" in time and has no support for any of this.

You have two options:

  1. Use the autoloader of the latest version and also include Namespace (requires at least PHP 5.3). Watch out for 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(); 
            } 
    }
    
  2. Use an older version with the library structure similar to the tutorial you are using (in the case of 0.8.1). Folder structure is the same:

https://github.com/PHPOffice/PHPWord/tree/0.8.1/Classes

inserir a descrição da imagem aqui

PS.: Try to keep up with what’s new in PHP before you start developing. I’m sorry to tell you, you’re already generating obsolete code if you’re working on a new project with Codeigniter.

A north of how to get well is the PHP: The Right Way.

  • Thanks for the suggestion @gmsantos, I will test as soon as possible, and thanks also for the tip, I will take a look at the link Voce recommended me.

  • @Thecoder if the answer is useful, you can either mark as an answer to your question or vote positively. Learn more by doing a [tour] and welcome to Stack Overflow!

  • It worked, thanks for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.