News

Using phpdocx with namespaces

  • Oct 09, 2013

Warning

This post is outdated. For up date information about using phpdocx with namespaces please refer to the Namespaces in phpdocx.

phpdocx is compatible with PHP 5.2. Due to this, phpdocx doesn't include namespaces in its classes. If you're using a PHP 5.3 framework or developing a custom project that need to use namespaces, you can add namespaces to the library using the UniversalClassLoader.php file (included in the phpdocx package). This file implements an "universal" autoloader for PHP 5.3 and greater versions.

The use of this class with phpdocx is very easy:

1. Include the universal class loader and phpdocx:
 

require_once '../../lib/UniversalClassLoader.php';
require_once '../../classes/Phpdocx_Phpdocx.inc';


2. Init UniversalClassLoader and assign a prefix to phpdocx:

 

 

$loader = new UniversalClassLoader();
$loader->registerPrefixes(array( 'Phpdocx_' => dirname(__FILE__).'/../classes', ));
$loader->register();


3. Instanciate phpdocx using the previous prefix:

 

 

 

 

$docx = new \Phpdocx_Phpdocx();
$docx->addText('This is a new text');
$docx->createDocx('../docx/example_namespaces');


Please note you can only use this universal class loader if you're using PHP 5.3 and greater.