throwing exceptions in __autoload

php.internals

Robert Janeczek

22 years ago
both: __autoload and exceptions are great features in php5, but together they don`t seem to work well. i want to add some kind of error reporting to autoload which will throw exception if class file wasn`t found in my predefined directories. i`ve prepared my own exception class which should show me nice little error message. what i get is: Fatal error: __autoload(dupa) threw an exception of type 'Exception' in [blah blah] on line 87 Fatal error: Exception thrown without a stack frame in Unknown on line 0 the same happens when pure Exception class is used. 'try' block around full code doesn`t help. i use yesterday`s php5 windows build. should i report this as an error or exceptions should [not] work like this? rash

Robert Janeczek

22 years ago
i found http://bugs.php.net/bug.php?id=26193 which explains my problem, but... the reason of problems with catching this exception seems to be no stack frame added. can i set it by myself somehow in derived exception class? my idea to working around this problem is to create temporary class (not object), but i`m not sure if it can be done. in case of error i would create string with class declaration, 'eval' it and throw exception. specified exception for this problem would accept class name as one of params and undeclare the class. i can`t however find any way of undeclaring/redeclaring classes - is it possible? rash

Andi Gutmans

22 years ago
Don't have access to a running version of PHP right now (on vacation) but by design __autoload() isn't supposed to propagate an exception. It's your last chance to load the class and if it fails, then it dies. If this isn't exactly what's happening then there's a problem. The reason for this is, that due to __autoload() being called from quite a few different contexts it is not clear (at least I can't quite remember) how this will affect the run-time. It might end up being very problematic as some code also assumes that it succeeds. I personally think, that in most cases, there aren't very good reasons to throw exceptions from it. How come you can't declare the class when __autoload() is called but you can do so later? In such a weird situation, you could declare a proxy object with the same name which relays all calls to the real class if/when you can load it. So basically, I understand what you're saying, but I don't see a very compelling reason to work very hard now to make this work differently. Andi At 05:38 PM 6/19/2004 +0200, Robert Janeczek wrote:

Robert Janeczek

22 years ago
> most cases, there aren't very good reasons to throw exceptions from it.
How
> come you can't declare the class when __autoload() is called but you can
do
> so later? In such a weird situation, you could declare a proxy object with > the same name which relays all calls to the real class if/when you can
load it.
> So basically, I understand what you're saying, but I don't see a very > compelling reason to work very hard now to make this work differently.
i`m working on application server for php. i need exception to continue server running after a component couldn`t be found or properly loaded, or at least close it with some trash cleaning. i wanted to write as few code in __autoload as possible, but it seams that i`ll have to check all the possibilities of crashing. rash ps. yes, i know php is supposed to work in a different way than java :] i just love challenges ;)