test.ical.ly | getting the web by the balls



PHP 5.3 and the Symfony2 UniversalClassLoader – Where to load?

While working on my rewrite of the ImageTransform library I decided to follow the PHP Standards Working Group proposal for naming schemes as well as using the new Symfony2 UniversalClassLoader.

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.

· ·

  • So using your approach, developers using your library will *have* to set up autoloading themselves. Not a problem if they use one of the many available frameworks as these require/set up an autoloader, but what if the library is used outside of one of these frameworks.

    The reason I included the UniversalClassLoader in my library was because I can not assume the user has set up autoloading. Using your approach, this would mean they’d have to go through a lot of steps of setting up an autoloader to match your autoloading criteria.

    Until the UniversalClassLoader is part of SPL (and with that comes with PHP), using my approach seems like a logical approach. I had no problems with writing unit tests (I did that without the UCL), and my current approach allows for usage of the library as a stand-alone library in a non-framework project as well as easy usage inside a framework-driven project.

    I am still investigating the setup though. A good compromise was suggested to me by (IIRC) Dennis Benkert, who suggested to move the autoloading code that is currently in the OpenNOS class file to a seperate autoload.php, which would add a single step (including the autoload.php) to the installation path, with that removing it from my main class file. I have to try some thing but this seems a valid approach, and a good compromise between my current approach and your approach.

  • @Stefan Maybe I wasn’t clear. I am not going to remove the class loader itself from the library but I want to remove the dependency. So instead of calling the class loader from the lib code I want the user to be able to choose to use it or to use something else.
    I still need the loader anyhow for the unit tests and I already have a bootstrap file for those that takes care of only the auto loading. So maybe Dennis suggestion is the right compromise. I could rename my test/bootstrap.php to src/autoload.php and be done with it.

  • So I did it. removed the calls from the library code and renamed the bootstrap.php to autoload.php.
    See: http://bit.ly/gVEsnt

  • Pingback: Tweets that mention • PHP 5.3 and the Symfony2 UniversalClassLoader – Where to load? | test.ical.ly -- Topsy.com()

  • Pingback: Test.ical.ly Blog: PHP 5.3 and the Symfony2 UniversalClassLoader - Where to load? | Development Blog With Code Updates : Developercast.com()



Theme Design by devolux.nh2.me