Re: __autoload & exceptions

From: Date: Sun, 12 Oct 2003 19:30:19 +0000
Subject: Re: __autoload & exceptions
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-4805@lists.php.net to get a copy of this message
Hi Andi- Yes, that explains things and makes perfect sense if you are just writing your own web application. Now, consider the new __autoload functionality from a PEAR or other large API/library developer standpoint. The new __autoload functionality lets us: - Keep library usage/installation simple by only requiring the user to include one include file regardless of how many possible classes or other files may be needed. This 'initialization' file will setup a special autoloader for just that library. If the user happens to use their own autoloader, then they have to simply setup something like- if (eregi('^libclass_', $className)) { somelib::autoload($className); } A 1, with an optional 2 step install process, regardless of which parts of the library may be used is a good thing. :) - Keeps the performance of the library up because it now only needs to load class objects the user actually uses. The new exception handling also lets a library developer easily document and let a user know that mylib will throw exception x, y and z. With that in mind, it's always good practice when writing an API/library to throw the most informative errors possible. Normally, this would also include errors such as "Unable to initialize library 'mylib' because file 'foo.inc' could not be found. Please check install documentation.". A much nicer exception/error than Fatal Error in mylib. This would also allow the user to catch the error and handle it themself, and at their sole discretion choosing to continue on with script execution. These are just my thoughts and observations after writing a large API/library in PHP5- Dan Cox Andi Gutmans wrote:
Dan, __autoload() is the last chance to load a class which is needed before erroring out and terminating the script. By design, if you don't take advantage of this last chance the script will error out. Therefore, throwing an exception inside the __autoload() function should not be catchable by your script. I hope that explains things. Andi


« previous php.internals (#4805) next »