Using one namespace twice in the same runtime

623 views Asked by At

I have two separate PHP modules each containing a vendor folder created by composer. Both modules use the composer autoloader which is registered before any of the module's class is instantiated.

Each vendor folder contains a different version of Doctrine, so in the same runtime, if I instantiate any Doctrine class from one module, any subsequent calls to the same Doctrine class from the other module, will use the first instantiated class as it is already autoloaded by PHP (it has same namespace, same name).

Is there a way to go around this without actually renaming all the namespaces and use declarations of one or both of the different Doctrine libraries?

1

There are 1 answers

0
Sven On BEST ANSWER

No, there is no way to use two versions of the same classes in one runtime. This is independent of autoloading, it's PHP basics:

If a class of name "X" is defined, no autoloading will trigger to load it again. And it cannot be included a second time manually because that will result in an error (classes, as well as functions, can only be declared once).

Try not to use two different versions of Doctrine. The best solution would be to add both your PHP modules to the main application using Composer itself. That way, both modules would declare their dependencies (among them Doctrine), and Composer would try to load the best possible version - but only ONE version. If this is impossible because of incompatible version constraints, you will know and then have to update your code.