On Sun, Jul 5, 2009 at 10:21 PM, Ben Bidner <b...@vuetec.com> wrote:

> > per the manual, exceptions thrown in an autoload method are swallowed,
> > and an E_ERROR is triggered by php.
> >
> > http://us2.php.net/manual/en/language.oop5.autoload.php
>
> I have read that note before, and wondered exactly what it was referring to
> since you can throw exceptions within an autoloader and catch them (or have
> your exception handler do whatever it needs to do with them).
>
> <?php
>
>   function autoloader($className)
>   {
>      echo "autoloading" . PHP_EOL;
>       throw new Exception("Fail");
>   }
>
>   spl_autoload_register("autoloader");
>
>    try
>   {
>      // Exception
>      $obj = new NonExistentClass;
>   }
>   catch(Exception $e)
>   {
>      echo "caught" . PHP_EOL;
>   }
>
>   try
>   {
>      // Exception
>      $const = constant("NonExistentClass::NON_EXISTENT_CONSTANT");
>   }
>   catch(Exception $e)
>   {
>      echo "caught" . PHP_EOL;
>   }
>
>   try
>   {
>      // Fatal error
>      $const = NonExistentClass::NON_EXISTENT_CONSTANT;
>   }
>   catch(Exception $e)
>   {
>      echo "never happens" . PHP_EOL;
>   }
> ?>
>
> Will output:
>
> autoloading
> caught
> autoloading
> caught
> autoloading
> PHP Fatal error: Undefined class constant


on both my systems (mentioned in reply to rob) the script fatals after the
first "autoloading", just like the manual says..

nat...@trident2:~$ php testcode.php
autoloading

Fatal error: Class 'NonExistentClass' not found in /home/nathan/testcode.php
on line 41

-nathan

Reply via email to