theseer / Autoload

A lightweight php namespace aware autoload generator and phar archive builder
Other
388 stars 47 forks source link

Add template that throws an exception in case a class cannot be loaded #9

Closed sebastianbergmann closed 13 years ago

sebastianbergmann commented 13 years ago

The default behaviour of a callback that is registered with spl_autoload_register() is to not do anything in case it cannot load the requested class. This is because, by default, a queue of autoload callbacks is desired. When none of the registered autoload callbacks can load the class, PHP triggers a fatal error that cannot be caught.

In the case where only one autoload callback is used it might make sense to raise an exception inside the autoload callback when it cannot load the class. That way the application can perform proper error handling.

theseer commented 13 years ago

After thinking about this for quite a while, I do not see the benefit of such a template: To make actual use of it, you'd need to have a generic exception handler (possibly registered as global exception handler) or write code like this: try { new Foo(); } catch (AutoloadException $e) {.... } which somehow looks rather odd and overengeneered. It also implies that it's an expected behavior that the class might not get instantieted, which should be a very unusual edgecase at development time only.

So for now, I don't think it should be implemnted.