2

I have downloaded a very small sample file that I am using while trying to get PHPWord to work. I am getting the following error message and can't figure out why.

PHP Fatal error: Class 'PHPWord' not found in /var/www/html/PhpOffice/ex_01.php on line 6

My downloaded small sample program is:

<?php
  require_once 'PhpWord.php';

  echo realpath (dirname(__FILE__)) . "\n";

  $PHPWord = new PHPWord();     <-- <-- <-- LINE 6 HERE
  $section = $PHPWord->createSection();
  $section->addText('Hello world!');
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-Disposition: attachment; filename=helloWorld.docx");
  header("Content-Type: application/docx");
  header("Content-Transfer-Encoding: binary");
  $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
  $objWriter->save('helloWorld.docx');
?>

The internet distributed PhpWord.php file is a bit large for posting but the key point is that lines 43 and 44 and lower contain class code with matching spelling and matching upper/lower case:

class PHPWord
{

For debugging purposes I am running the code from the command line. This seems to give me meaningful error messages.

The "require" seems to be finding the file. The "echo dirname" is posting " /var/www/html/PhpOffice" which is the correct location of both files. In addition, as an experiment I added a typo to the file name in the "require" statement and this caused a "file not found" error. Thus, the "require" looks good.

I have some experience with PHP but this is the first time I have read about classes and namespace in PHP. So far I have no idea what is going wrong. Any suggestions?

Thank you, Bruce

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Bruce32901
  • 21
  • 1
  • 3
  • IIRC, I recently solved a similar problem by checking the source code, and noticing it was opened with `` instead of ` – HC_ Jan 21 '16 at 22:41
  • 2
    PHPWord is namespaced.... you need to reference the class by its full namespaced name.... `$phpWord = new \PhpOffice\PhpWord\PhpWord();` Try looking at some of the Examples in the `/samples` folder – Mark Baker Jan 21 '16 at 22:43
  • 1
    With new \PhpOffice\PhpWord\PHPWord(); I am now getting: PHP Fatal error: Class 'PhpOffice\PhpWord\Settings' not found in /var/www/html/PhpOffice/ex_01.php on line 6. I will have to do some digging but I suspect that my first problem is solved and I am now failing on lower level calls. I am still a bit confused with namespace and obviously I don't have something right in my install if the distributed PhpWord files don't run due to internal code. – Bruce32901 Jan 21 '16 at 22:57

2 Answers2

1

first time I have read about classes and namespace in PHP

If in the PhpWord.php is something like namespace XYZ; Then you have too do place use \XYZ\PHPWord; in the first lines of your script.

If you are within a namespace and the class has no, then you have to use $PHPWord = new \PHPWord();

If you dont have an autoloader you have too include all the files that belonging to PHPOffice and then you can use use \PHPOffice\PHPWord();

ob_start
  • 46
  • 2
  • I am still trying to grasp the namespace issues and pitfalls but here are the only "use" and "namespace" lines that I found in PhpWord.php. – Bruce32901 Jan 21 '16 at 22:47
  • namespace PhpOffice\PhpWord; use PhpOffice\PhpWord\Element\Section; use PhpOffice\PhpWord\Exception\Exception; – Bruce32901 Jan 21 '16 at 22:47
  • So even if the class file is found I still need to add a "use" to allow the class within the class file to be loaded? – Bruce32901 Jan 21 '16 at 22:49
  • Can you build a Autoloader? – ob_start Jan 21 '16 at 22:50
  • I saw Autoloader in several postings on the Internet but so far I have not read any Autoloader tutorials and don't have any idea how to build, run or debug one. – Bruce32901 Jan 21 '16 at 22:51
  • I'll read up on Autoloader and give that a try. I have to leave the computer for a few hours. Thank you everyone for the help so far and I'll post my results or refined questions as soon as I can. – Bruce32901 Jan 21 '16 at 23:01
  • 1
    Note that PHPWord is written for installation with composer and use of the composer autoloader – Mark Baker Jan 21 '16 at 23:09
  • I would simply use the [composer autoloader](https://getcomposer.org/doc/01-basic-usage.md#autoloading) instead of building something myself. By using existing tools I have more time for the things that make my apps unique. – Arjan Jan 21 '16 at 23:23
-1

The solution for remote server (I fixed my issue for GoDaddy):

If you are using cpanel you may have zip extension installed but not activate. You need to active it. For this case you need to go to cpanel > inside software section > click on PHP version. Then find zip and check it. Now save.

You should see like the image.

Refresh your application page.

Shafiq
  • 963
  • 8
  • 4
  • Why should a PHP class named `PHPWord` became available after activating a PHP extension? That does not make any sense – Nico Haase May 08 '18 at 13:30
  • Nico Haase why you down voted? It works for me without any problem by enabling the zip extension. – Shafiq May 09 '18 at 09:24
  • As I said: according to the given error message (and not any other similar use case that only looked the same but was completely different), enabling a PHP extension can not be the core solution – Nico Haase May 09 '18 at 09:25