PHP 5.3 and the Symfony2 UniversalClassLoader – Where to load?
But I’m not yet sure what is the best approach to use this loader.
When I started looking for examples I sent a small tweet which was replied to by Stefan Koopmanschap as he used this class loader in one of his own libraries.
What he did is to include the class/namespace registering code in one of the central classes of his library. If you take a look at my code you will see the same approach.
However this will not deliver instant plug’n'play imho as you still have to require this one class file to trigger the registration.
The more I think about the more I come to the conclusion that it is not advisable to include automatic class loading in library code as the user of your library still has to do it on his own even if you do some of it for him. This also puts the decision what kind of loading mechanism to use back in his hands.
The library code should assume that there is a loading mechanism in place thus not implementing any of its own nor hard wiring with require().
I actuaally realized that when I wrote the bootstrap file for my PHPUnit tests. Unit tests are client code that use parts of the library they test. If they would have to include this central class only to have all other classes of the library available then this would be a case of coupling that I wanted to get rid of in the first place.
So instead of including calls to UniversialClassLoader I will eventually only mention it in the docs I think.