php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26193 Exceptions from within _autoload cannot be caught
Submitted: 2003-11-10 17:17 UTC Modified: 2004-12-28 10:49 UTC
Votes:33
Avg. Score:4.1 ± 1.3
Reproduced:24 of 29 (82.8%)
Same Version:6 (25.0%)
Same OS:15 (62.5%)
From: acm at tweakers dot net Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.0.0b2 (beta2) OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2003-11-10 17:17 UTC] acm at tweakers dot net
Description:
------------
When You try to implement some sort of class-loader, the autoload-function is very usefull.
But as soon as you want the user-code to do something special when a class isn't found, it'd be handy to throw out an exception, which the user-code either can catch or ignore.

With the current beta2 that isn't possible as it seems.

Reproduce code:
---------------
<?
    function __autoload($class)
    {
        throw new Exception($class . " does not exist.");
    }

try
{
    $obj = new test(); // Where test is a non-existent class
}
catch(Exception $e)
{
    echo $e;
}
?>

Expected result:
----------------
Something like:
exception 'exception' with message 'test does not exist.' etc..

Actual result:
--------------
Fatal error: __autoload threw an exception in /home/acm/public_html/kb/autoload.php on line 11

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-10 19:28 UTC] helly@php.net
For internal reasons there is nothing else the executor can do, so catching such an exception will leave the executot in an unstable state. Hence there is no exception.

Only in case of class_exists() and exception would be possible. But i don't think someone will find a good solution for that and goes implementing it.
 [2004-12-28 10:49 UTC] mfischer@php.net
On http://de.php.net/manual/en/language.oop5.autoload.php a nice workaround was posted:

The following might provide a good work-around for throwing exceptions from the __autoload function when a file containing the correct class doesn't exists.

function __autoload ($class_name) {
  $file = 'system/objects/' . $class_name . '.inc.php';
  if (!file_exists ($file)) {
   return eval ("class $class_name {" .
                 "  function $class_name () {" .
                 "    throw new Exception ();" .
                 "  }" .
                 "}");
  }
  require_once ($file);
}

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC