0

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?

3
  • 1
    This begs the question why do you need to load 2 Doctrines, that's just bad to begin with. Commented Jun 22, 2015 at 12:35
  • "Each vendor folder contains a different version of Doctrine" Commented Jun 22, 2015 at 12:36
  • If you rename all namespaces of at least one doctrine it could be possible. But composer update will kill you. You can't have two versions of one library in one project in php. Update one of your projects so both are compatible with the same doctrine version would be a clean solution. Everything else will just be bad. Commented Jun 22, 2015 at 12:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.