Cookbook

Namespaces in phpdocx

Since the release of version 5.3, PHP counts with namespaces support (http://php.net/manual/en/language.namespaces.php). Thanks to those namespaces, libraries developed in PHP have their own scope of execution and do not generate conflicts due to several files with the same class name.

The phpdocx packages of Basic license and the Trial version don't support namespaces. Advanced and Premium licenses include two packages: one without namespaces and the other with namespaces.

The packages without namespaces are compatible with PHP 5.2 or higher (including PHP 7). The packages with namespaces require PHP 5.3 or higher (including PHP 7).

To work with the namespaces packages is directly related to the project a user can develop, nor is it the PHP version used for it.

For example, if you are going to start a development from scratch, you're free to choose either phpdocx package.

Tip

Anyway, the package with namespaces is the most advisable choice, as it will elude conflicts with libraries or classes external to phpdocx.

If you are going to work with a CMS or a framework, find first if it uses namespaces or not, e.g., Wordpress and Drupal 7 don't require namespaces, but others like Drupal 8, Symfony 2, Laravel or Yii do.

Finally, if you have PHP 5.2 installed, you should choose the version without namespaces. If you are running PHP 5.3 or higher, you can work either the package with namespaces or the one without them.

This is the simplest possible code that shows how to generate a DOCX with the package with namespaces:

All the examples included in the packages that support namespaces in the Advanced and Premium licenses make use of these namespaces.