在1348行的app / code / core / Mage / Core / Model / Config.php中找不到类'PhpOffice \ PhpWord \ Settings'

After upgraded to PHP 7, my website magento print order using PhpWord library not working.

I have this error:

Cannot use PhpOffice\PhpWord\Shared\String as String because 'String' is a special class name in /home/.../vendor/phpoffice/phpword/src/PhpWord/Style/Paragraph.php

Then I using lastest PhpOffice/PhpWord and PhpOffice/Common but it's showing next error:

Fatal error: Class 'PhpOffice\PhpWord\Settings' not found in /app/code/core/Mage/Core/Model/Config.php on line 1348

Can you help me solve this error?

Solution:

Rename the file phpoffice/phpword/src/PhpWord/Shared/String.php into phpoffice/phpword/src/PhpWord/Shared/StringHelper.php. Use a texteditor which has a search and replace feature over multiple files or a command shell tool like find or sed in order to search for lines use PhpOffice\PhpWord\Shared\String; and replace each by a line use PhpOffice\PhpWord\Shared\StringHelper; . This means that the whole word String must be replaced by StringHelper on all places in all the PHP files. Search also for String:: and replace it by 'StringHelper::` .

Explanation:

The class String seems to be reserved by PHP and not even a namespace is allowed. Solution: Use the class name StringHelper instead of it.

Linux Shell:

find . -name '*.php'  -exec grep -nw String {} \; -ls

This will list you all the files and the line numbers where you must make the replacements.

Solution 1: I renamed class String to Stringg in entire phpword library and this works for me with php7 and older version of php too..

Solution 2: or one can use by giving alias to string class like

use PhpOffice\PhpWord\Shared\String as POString;

And now use POString as mentioned below.

public function setText($text)
{
$this->text = POString::toUTF8($text);
return $this;
}

This can also works fine