# [RFC] Autoloader Error Handling

- Thread: https://externals.io/message/56602
- Original archive: https://news-web.php.net/php.internals/56602
- Started by Christian Kaps on 2011-11-24T22:28:35+00:00
- Messages: 20
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #56602 — Christian Kaps — 2011-11-24T22:28:35+00:00

URL: https://externals.io/message/56602#56602

Hi internals,

I've written a RFC about the optimization of the autoloader error handling.

Please take a moment to review the RFC and post any questions, suggestions or concerns here.

https://wiki.php.net/rfc/autoloader_error_handling

Cheers,
Christian

---

## #56614 — Michael Wallner — 2011-11-25T07:24:59+00:00

URL: https://externals.io/message/56602#56614 · In reply to: #56602

> 
> https://wiki.php.net/rfc/autoloader_error_handling
> 

Throwing an exception or fatal error in an autoloader
absolutely does not make any sense in my eyes.
Projects doing this should step back and think a 
minute about what they dare.

Mike

---

## #56615 — Christian Kaps — 2011-11-25T08:06:33+00:00

URL: https://externals.io/message/56602#56615 · In reply to: #56614

Am 25.11.2011 08:24, schrieb Michael Wallner:
> 
>
> > […]
>
> Throwing an exception or fatal error in an autoloader
> absolutely does not make any sense in my eyes.
> Projects doing this should step back and think a
> minute about what they dare.
>
> Mike

Hi,

how would you bring your application in a consistent state after a 
class couldn't be loaded. I do this by adding a try/catch block around 
my code.

try {
     new Application();
} catch (Exception) {
     // collect data
     // send mail
     // redirect to maintenance page
}

An other question is, if the autoloader work silent and I write:

new NotExistingClass();

I think in this case the engine will also trigger a fatal error. So in 
my eyes it is regardless of whether it trigger a fatal error in the 
autoloader or the autoloader works silent. Both cases ends in a fatal 
error. Or am i wrong here?

Christian

---

## #56616 — Sebastian Bergmann — 2011-11-25T08:55:08+00:00

URL: https://externals.io/message/56602#56616 · In reply to: #56615

Am 25.11.2011 09:06, schrieb Christian Kaps:
> Or am i wrong here?

 Yes you are. The idea is that you can have multiple autoload callbacks
 which are invoked in sequence. The first one that is able to load the
 requested class will end that sequence. If you throw exceptions in one
 autoloader that sequence will be interrupted prematurely.

-- 
Sebastian Bergmann                    Co-Founder and Principal Consultant
http://sebastian-bergmann.de/                           http://thePHP.cc/

---

## #56617 — Sebastian Krebs — 2011-11-25T08:56:01+00:00

URL: https://externals.io/message/56602#56617 · In reply to: #56615

Hi,

Just to throw my 2 cent in: Im with Micheal. An application, that tries to
access a class, that doesn't exists, is broken and a FATAL is valid. This
application doesn't need try-catch, but a bugfix (and if it is already
released: A better testing management).
On the other side an application, that makes use of dynamic class names
should make use of `class_exists()` in any case. An exception after calling
 `class_exists()` is just bad, but the classloader cannot distinguish between
the reasons, why it is called.

2011/11/25 Christian Kaps <christian.kaps@mohiva.com>

> Am 25.11.2011 08:24, schrieb Michael Wallner:
>
>  
> > […]
>
> Hi,
>
> how would you bring your application in a consistent state after a class
> couldn't be loaded. I do this by adding a try/catch block around my code.
>
> try {
>    new Application();
> } catch (Exception) {
>    // collect data
>    // send mail
>    // redirect to maintenance page
> }
>
> An other question is, if the autoloader work silent and I write:
>
> new NotExistingClass();
>
> I think in this case the engine will also trigger a fatal error. So in my
> eyes it is regardless of whether it trigger a fatal error in the autoloader
> or the autoloader works silent. Both cases ends in a fatal error. Or am i
> wrong here?
>
> Christian

---

## #56618 — Christian Kaps — 2011-11-25T09:13:39+00:00

URL: https://externals.io/message/56602#56618 · In reply to: #56616

Am 25.11.2011 09:55, schrieb Sebastian Bergmann:
> Am 25.11.2011 09:06, schrieb Christian Kaps:
> > […]
>
>  Yes you are. The idea is that you can have multiple autoload 
> callbacks
>  which are invoked in sequence. The first one that is able to load 
> the
>  requested class will end that sequence. If you throw exceptions in 
> one
>  autoloader that sequence will be interrupted prematurely.
>

For this reason I have written the RFC.

---

## #56620 — Rafael Kassner — 2011-11-25T09:34:15+00:00

URL: https://externals.io/message/56602#56620 · In reply to: #56615

I think an autoloader can't be raise any kind of error. If the class
will be loaded by another autoloader on the stack and the first throws
an Exception, this will be a wrong behavior.
Indeed, if autoloader can't include the class, PHP throws a fatal
error, and you can avoid it using class_exists function that calls
autoloader by its own.
On Fri, Nov 25, 2011 at 6:06 AM, Christian Kaps
<christian.kaps@mohiva.com> wrote:
> Am 25.11.2011 08:24, schrieb Michael Wallner:
> > […]
>
> Hi,
>
> how would you bring your application in a consistent state after a class
> couldn't be loaded. I do this by adding a try/catch block around my code.
>
> try {
>    new Application();
> } catch (Exception) {
>    // collect data
>    // send mail
>    // redirect to maintenance page
> }
>
> An other question is, if the autoloader work silent and I write:
>
> new NotExistingClass();
>
> I think in this case the engine will also trigger a fatal error. So in my
> eyes it is regardless of whether it trigger a fatal error in the autoloader
> or the autoloader works silent. Both cases ends in a fatal error. Or am i
> wrong here?
>
> Christian
>
> --
> 
> 
>
>



-- 
Atenciosamente,
Rafael Kassner

---

## #56621 — Christian Kaps — 2011-11-25T09:37:06+00:00

URL: https://externals.io/message/56602#56621 · In reply to: #56617

Hi,

comments inline.


Am 25.11.2011 09:56, schrieb Sebastian Krebs:
> Hi,
>
> Just to throw my 2 cent in: Im with Micheal. An application, that 
> tries to
> access a class, that doesn't exists, is broken and a FATAL is valid. 
> This
> application doesn't need try-catch, but a bugfix (and if it is 
> already
> released: A better testing management).

How you will be informed about that the application breaks? OK, you can 
do this with a good log management tool or a log server. But the easiest 
way is to write a mail. An other problem is that the user sees mostly a 
white page on a fatal error. An other advantage of the exception 
approach is that you can collect data from session, from request or from 
server and send this with the mail. With a fatal error this isn't 
possible.

Your objection with the test management is valid. But I'd rather be 
insured against all accidents.

The problem is that the engine allows to throw exceptions. So either we 
need a solution to handle all cases or it shouldn't possible to throw 
exceptions. The particular approaches shouldn't be the problems here.

> On the other side an application, that makes use of dynamic class 
> names
> should make use of `class_exists()` in any case. An exception after 
> calling
> `class_exists()` is just bad, but the classloader cannot distinguish 
> between
> the reasons, why it is called.
>
> 2011/11/25 Christian Kaps <christian.kaps@mohiva.com>
>
> > […]

---

## #56622 — Christian Kaps — 2011-11-25T09:46:25+00:00

URL: https://externals.io/message/56602#56622 · In reply to: #56620

Am 25.11.2011 10:34, schrieb Rafael Kassner:
> I think an autoloader can't be raise any kind of error. If the class
> will be loaded by another autoloader on the stack and the first 
> throws
> an Exception, this will be a wrong behavior.
> Indeed, if autoloader can't include the class, PHP throws a fatal
> error, and you can avoid it using class_exists function that calls
> autoloader by its own.

You can do this, but the engine triggers still a fatal error. The call 
"new NotExistingClass();" triggers the autoloader. The autoloader sees 
that the class doesn't exists and continues silent. And now the engine 
tries to instantiate a not existing class, which ends in a fatal error.


> On Fri, Nov 25, 2011 at 6:06 AM, Christian Kaps
> <christian.kaps@mohiva.com> wrote:
> > […]
>
>
>
> --
> Atenciosamente,
> Rafael Kassner

---

## #56624 — Rafael Kassner — 2011-11-25T10:00:16+00:00

URL: https://externals.io/message/56602#56624 · In reply to: #56622

If your autoloader throws an exception, you need to try/catch entire
application. It doesn't sounds useful. On my view, if you want to be
mailed about an autoloader fail, this have to be coded inside of your
own single autoloader.

On Fri, Nov 25, 2011 at 7:46 AM, Christian Kaps
<christian.kaps@mohiva.com> wrote:
> Am 25.11.2011 10:34, schrieb Rafael Kassner:
> > […]
>
> You can do this, but the engine triggers still a fatal error. The call "new
> NotExistingClass();" triggers the autoloader. The autoloader sees that the
> class doesn't exists and continues silent. And now the engine tries to
> instantiate a not existing class, which ends in a fatal error.
>
>
> > […]
>
>
> --
> 
> 
>
>



-- 
Atenciosamente,
Rafael Kassner

---

## #56627 — Ferenc Kovacs — 2011-11-25T10:15:28+00:00

URL: https://externals.io/message/56602#56627 · In reply to: #56617

The problem with fatal, that you have no way (by the standard means, but
you can somehow manage it through the usage of output buffers or
register_shutdown_function, but thats ugly, and can change in the future)
to intercept and gracefully terminate your application, which is an often
needed feature.
AFAIK E_FATAL should be only used where the engine is left in an unstable
state, which isn't really true here.
So I think that `E_RECOVERABLE_ERROR` would be more appropriate here, and in
general we use `E_ERROR` many places where `E_RECOVERABLE_ERROR` would be more
suitable, but thats another topic.

For clarification: I'm talking about the E_FATAL which currently will be
called if none of the registered autoloaders were able to load the
requested class.

On Fri, Nov 25, 2011 at 9:56 AM, Sebastian Krebs
<krebs.seb@googlemail.com>wrote:

> Hi,
>
> Just to throw my 2 cent in: Im with Micheal. An application, that tries to
> access a class, that doesn't exists, is broken and a FATAL is valid. This
> application doesn't need try-catch, but a bugfix (and if it is already
> released: A better testing management).
> On the other side an application, that makes use of dynamic class names
> should make use of `class_exists()` in any case. An exception after calling
> `class_exists()` is just bad, but the classloader cannot distinguish between
> the reasons, why it is called.
>
> 2011/11/25 Christian Kaps <christian.kaps@mohiva.com>
>
> > […]
> https://wiki.php.net/rfc/autoloader_error_handling>
> > […]
> autoloader
> > […]
>



-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #56628 — Sebastian Krebs — 2011-11-25T10:21:30+00:00

URL: https://externals.io/message/56602#56628 · In reply to: #56627

2011/11/25 Ferenc Kovacs <tyra3l@gmail.com>

> The problem with fatal, that you have no way (by the standard means, but
> you can somehow manage it through the usage of output buffers or
> register_shutdown_function, but thats ugly, and can change in the future)
> to intercept and gracefully terminate your application, which is an often
> needed feature.
> AFAIK E_FATAL should be only used where the engine is left in an unstable
> state, which isn't really true here.
>

Trying to interact with a non-existing class is not an unstable state?

there are two situations:

1. The application is broken, thus the FATAL is appropriate
2. The application tries to work with dynamic class names. If it doesn't
use `class_exists()` before, its broken and FATAL is appropriate


So I think that `E_RECOVERABLE_ERROR` would be more appropriate here, and in
> general we use `E_ERROR` many places where `E_RECOVERABLE_ERROR` would be more
> suitable, but thats another topic.
>
> For clarification: I'm talking about the E_FATAL which currently will be
> called if none of the registered autoloaders were able to load the
> requested class.
>
> On Fri, Nov 25, 2011 at 9:56 AM, Sebastian Krebs <krebs.seb@googlemail.com
> > […]
>
> > […]
>
>
>
> --
> Ferenc Kovács
> @Tyr43l - http://tyrael.hu

---

## #56629 — Ferenc Kovacs — 2011-11-25T10:44:30+00:00

URL: https://externals.io/message/56602#56629 · In reply to: #56628

On Fri, Nov 25, 2011 at 11:21 AM, Sebastian Krebs
<krebs.seb@googlemail.com>wrote:

> 2011/11/25 Ferenc Kovacs <tyra3l@gmail.com>
>
> > […]
>
> Trying to interact with a non-existing class is not an unstable state?
>

"where the __engine__ is left in an unstable state"
trying to interact with a non-existing class could push the __application__
in an unstable state, but changing `E_ERROR` to `E_RECOVERABLE_ERROR`  would
give a chance for those developers/applications to gracefully terminate.


> there are two situations:
>
> 1. The application is broken, thus the FATAL is appropriate
>

so should we change all instance of E_NOTICE, `E_WARNING` and `E_STRICT` to
E_ERROR?
usually those also mean that the code is broken...


> 2. The application tries to work with dynamic class names. If it doesn't
> use `class_exists()` before, its broken and FATAL is appropriate
>

same here.

I agree that trying to load an non-existing class is a bug, but `E_ERROR` is
not a punishment for buggy code, it is a safety measure, when there is no
safe way to give back the control to the application, because the engine
for that request is beyond repair.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #56630 — Christian Kaps — 2011-11-25T11:56:20+00:00

URL: https://externals.io/message/56602#56630 · In reply to: #56624

I surround my application with a try/catch block to catch uncaught 
exceptions. An autoloader exception could be one of them.

But I use also similar calls to reformat error messages.

try {
    $class = new ReflectionClass($annotationName);
} catch (ClassNotFoundException $e) {
    $message = "The annotation class `{$annotationName}` cannot be 
found; ";
    $message .= "called in DocBlock for: 
{$this->context->getLocation()}; ";
    throw new ClassNotFoundException($message, null, $e);
}

How would you write the above example without the exceptions? Maybe 
your solution is smarter then mine.




Am 25.11.2011 11:00, schrieb Rafael Kassner:
> If your autoloader throws an exception, you need to try/catch entire
> application. It doesn't sounds useful. On my view, if you want to be
> mailed about an autoloader fail, this have to be coded inside of your
> own single autoloader.
>
> On Fri, Nov 25, 2011 at 7:46 AM, Christian Kaps
> <christian.kaps@mohiva.com> wrote:
> > […]
>
>
>
> --
> Atenciosamente,
> Rafael Kassner

---

## #56632 — Christian Kaps — 2011-11-25T12:11:38+00:00

URL: https://externals.io/message/56602#56632 · In reply to: #56628

Can we stay on topic please. At this time I count a vast number of 
mails, but never has talked about the RFC.

Fact is that these three cases, how a autoloader terminates, exists in 
PHP. If all these cases are useful is an other topic. Can we all agree 
on this?


Am 25.11.2011 11:21, schrieb Sebastian Krebs:
> 2011/11/25 Ferenc Kovacs <tyra3l@gmail.com>
>
> > […]
>
> Trying to interact with a non-existing class is not an unstable 
> state?
>
> there are two situations:
>
> 1. The application is broken, thus the FATAL is appropriate
> 2. The application tries to work with dynamic class names. If it 
> doesn't
> use `class_exists()` before, its broken and FATAL is appropriate
>
>
> So I think that `E_RECOVERABLE_ERROR` would be more appropriate here, 
> and in
> > […]

---

## #56633 — Ferenc Kovacs — 2011-11-25T12:13:39+00:00

URL: https://externals.io/message/56602#56633 · In reply to: #56630

On Fri, Nov 25, 2011 at 12:56 PM, Christian Kaps
<christian.kaps@mohiva.com>wrote:

> I surround my application with a try/catch block to catch uncaught
> exceptions. An autoloader exception could be one of them.
>
> But I use also similar calls to reformat error messages.
>
> try {
>   $class = new ReflectionClass($**annotationName);
> } catch (ClassNotFoundException $e) {
>   $message = "The annotation class `{$annotationName}` cannot be found; ";
>   $message .= "called in DocBlock for: {$this->context->getLocation()**};
> ";
>   throw new ClassNotFoundException($**message, null, $e);
> }
>
>
if(!class_exists('ReflectionClass')){
  // call your error handler here or throw Exception as you like
}
else{
  $class = new ReflectionClass($**annotationName);
}

but in this particular case I don't see how could that class missing
(Reflection is a built in extension, always enabled).

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #56634 — Christian Kaps — 2011-11-25T12:28:30+00:00

URL: https://externals.io/message/56602#56634 · In reply to: #56633

Am 25.11.2011 13:13, schrieb Ferenc Kovacs:
> On Fri, Nov 25, 2011 at 12:56 PM, Christian Kaps
> <christian.kaps@mohiva.com>wrote:
>
> > […]
> if(!class_exists('ReflectionClass')){
>   // call your error handler here or throw Exception as you like
> }
> else{
>   $class = new ReflectionClass($**annotationName);
> }
>

Reflection class triggers also the autoloader, because it tries to load 
the class $annotationName.

---

## #56635 — Ferenc Kovacs — 2011-11-25T12:34:41+00:00

URL: https://externals.io/message/56602#56635 · In reply to: #56634

On Fri, Nov 25, 2011 at 1:28 PM, Christian Kaps
<christian.kaps@mohiva.com>wrote:

> Am 25.11.2011 13:13, schrieb Ferenc Kovacs:
>
> > […]
> Reflection class triggers also the autoloader, because it tries to load
> the class $annotationName.


then class_exists($annotationName)?

btw. I think that ReflectionClass::__construct will throw a LogicException
if the classname you passed couldn't be loaded.
so you don't need your special autoloader for handling that.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #56636 — Christian Kaps — 2011-11-25T12:45:53+00:00

URL: https://externals.io/message/56602#56636 · In reply to: #56635

Am 25.11.2011 13:34, schrieb Ferenc Kovacs:
> On Fri, Nov 25, 2011 at 1:28 PM, Christian Kaps
> <christian.kaps@mohiva.com>wrote:
>
> > […]
>
>
> then class_exists($annotationName)?

Oh, my mistake. I have forgotten that class_exists triggers also the 
autoloader. Yes, then your solution is as smart as mine.

>
> btw. I think that ReflectionClass::__construct will throw a 
> LogicException
> if the classname you passed couldn't be loaded.
> so you don't need your special autoloader for handling that.

---

## #56641 — Larry Garfield — 2011-11-26T02:58:49+00:00

URL: https://externals.io/message/56602#56641 · In reply to: #56628

> 2011/11/25 Ferenc Kovacs<tyra3l@gmail.com>
>
> > […]
>
> Trying to interact with a non-existing class is not an unstable state?
>
> there are two situations:
>
> 1. The application is broken, thus the FATAL is appropriate
> 2. The application tries to work with dynamic class names. If it doesn't
> use `class_exists()` before, its broken and FATAL is appropriate
>
>
> So I think that `E_RECOVERABLE_ERROR` would be more appropriate here, and in

There's a third possibility.

3) The application uses a mapped or indexed autoloader and the map is 
out of date.  Rebuilding the map would resolve the error.

So I could see a use for a recovery there; if new NonExistentClass 
fails, try to rebuild the index and try again before fataling out entirely.

Whether that's too much of an edge case or not, I don't know, but it is 
a legitimate use case.  (Drupal 7 has an indexed autoloader, and we've 
run into the stale index problem many times.  We're trying to migrate 
most code to PSR-0 for Drupal 8, but even that could use an index for 
caching/performance reasons.)

--Larry Garfield
