I have two modules (myModuleA and myModuleB) on a PrestaShop website that are using different version of the library phpofficephpspreadsheet.
The problem is that when I call a method from phpofficephpspreadsheet in myModuleA, it’s start using myModuleB phpofficephpspreadsheet method.
Example:
Call to a member function setActiveSheetIndex() on null
in modules/mymoduleb/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/Worksheet.php (line 1439)
in modules/mymodulea/test.php (line 33).
Is it possible to rename namespace from phpofficephpspreadsheet ?
I want to use phpofficephpspreadsheet like so :
use MyModuleAPhpOfficePhpSpreadsheetSpreadsheet;
and
use MyModuleBPhpOfficePhpSpreadsheetSpreadhsset;
2
Answers
You wouldn’t need to rename the namespaces, just use an alias:
use MyModuleAPhpOfficePhpSpreadsheetSpreadsheet as module_a_spreadsheet;
use MyModuleBPhpOfficePhpSpreadsheetSpreadsheet as module_b_spreadsheet;
Then you can use them independently:
module_a_spreadsheet::foo();
module_b_spreadsheet::foo();
More about aliasing: https://www.php.net/manual/en/language.namespaces.importing.php
Update – Based on your comment, it IS possible to create an alias for an entire namespace, but you would need to register the same new namespace alias for all additional classes that were called within the original class. You could use a combination
spl_autoload_register
andclass_alias
, but this seem like a huge waste in resource and a bit of overkill in my opinion. I haven’t tested the code below, but I think it would be a good place to start:I think the simplest solution is to just create a couple of variables and use those:
This is a common issue with the PrestaShop modules, where we don’t have one composer.json, but all modules load their dependencies.
The solution could be to prefix all your vendors, there are some tools to help you do that:
https://github.com/humbug/php-scoper
or PHP-Prefixer: https://github.com/PHP-Prefixer/hello-prestashop-world