Solving PhpWord Library Errors After Upgrading to PHP 7 on Magento
Are you facing issues with your Magento store’s order printing functionality after upgrading to PHP 7? You’re not alone. Let’s dive into the two main errors you’ve encountered and provide practical solutions.
Error 1: Cannot use PhpOffice\PhpWord\Shared\String as String
This error occurs because the String class is a reserved keyword in PHP, which conflicts with the namespace used by the PhpOffice/PhpWord library. To resolve this:
- Open the file
/vendor/phpoffice/phpword/src/PhpWord/Style/Paragraph.php. - Find the line where
use Stringis defined. - Change it to
use PhpOffice\PhpWord\Shared\String as WrappedString.
This rename will prevent the conflict with PHP’s built-in String class.
Error 2: Class ‘PhpOffice\PhpWord\Settings’ not found
This fatal error occurs because the Magento core is trying to use the old PhpOffice/PhpWord version that doesn’t have a Settings class. Here’s how to fix it:
-
Update your PHP libraries: Make sure you’re using the latest versions of both PhpOffice/PhpWord and PhpOffice/Common. You can update them via composer by running:
composer require phpoffice/phpword:latest composer require phpoffice/common:latest -
Clear Magento cache: After updating, clear your Magento cache to ensure the new library versions are loaded properly.
-
Check for conflicting packages: If the error persists, it might be due to a conflicting package in your
composer.jsonfile. Open this file and look for any other libraries that might interfere with PhpOffice/PhpWord. Ensure they are compatible with PHP 7. -
Reinstall Magento: As a last resort, if all else fails, you may need to reinstall Magento entirely. Before doing so, make sure to backup your data first. You can find detailed installation instructions on Laravel Company’s official guide.
Conclusion
Upgrading PHP versions often requires careful checking and updating of dependent libraries. In this case, you needed to update the PhpOffice/PhpWord library version and clear your Magento cache.
Remember, it’s always a good practice to test your website thoroughly after any major upgrade or update. This ensures that all functionalities work as expected without causing unexpected errors like the ones described above.
If you’re still facing issues or need further assistance with Magento development, feel free to explore our Magento services at Laravel Company for expert guidance and solutions tailored to your specific needs.