Jump to content

Throwing exception in __autoload();


Mchl

Recommended Posts

Manual says:

 

Note: Exceptions thrown in __autoload function cannot be caught in the catch block and results in a fatal error.

 

However when I run this code:

 

<?php
function __autoload($className) {
  if(file_exists("/classes/$className.class.php")) {  
    require_once("/classes/$className.class.php"); 
  } else {
throw new Exception("File /classes/$className.class.php does not exist"); 
  }

}

try {
$foo = new foo();
} catch(Exception $e) {
echo 'Exception caught:'.PHP_EOL;
echo $e->getMessage();
}

 

no fatal error occurs. Exception is handled as usual. I am doing this in 5.3 right now, but IIRC this was also a case in 5.2

Link to comment
https://forums.phpfreaks.com/topic/179679-throwing-exception-in-__autoload/
Share on other sites

I think (my memory isn't what it used to be) this might have been "fixed" for 5.3.0... I'll take a look-see for more info.

 

P.S. Yep looks like 5.2 still suffers (just tried 5.2.11) and 5.3 (tested on 5.3.0) works as your post says.  If that's the case, the note on the manual page will get amended.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.