php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50246 Throw exception fails in __autoload() implementation
Submitted: 2009-11-20 15:58 UTC Modified: 2009-11-20 22:40 UTC
From: krzysztof dot talajko at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.11 OS: Windows 7 RC
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: krzysztof dot talajko at gmail dot com
New email:
PHP Version: OS:

 

 [2009-11-20 15:58 UTC] krzysztof dot talajko at gmail dot com
Description:
------------
Throw exception in __autoload() implementation is impossible. Instead fatal error "Class (...) not found" is reported.

Reproduce code:
---------------
<?php

function loader($class) {
    @include_once($class.'.php');

    if (!class_exists($class, false)) {
        throw new Exception("Class ".$class." not found");
    }
}

spl_autoload_register('loader');

try{
  class FooClass extends Some_Not_Existing_Class {}
}
catch (Exception $e) {
  echo "Caught load class failed exception (".$e->getMessage().").";
}


Expected result:
----------------
Caught load class failed exception (Class Some_Not_Existing_Class not found).

Actual result:
--------------
Fatal error: Class 'Some_Not_Existing_Class' not found in C:\dev\workspace\foo.php on line 19

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-20 16:05 UTC] krzysztof dot talajko at gmail dot com
Simpler reproduce code is (results same as in original code):

<?php

function __autoload($class) {
    throw new Exception("Class ".$class." not found");
}

try{
  new Foo;
}
catch (Exception $e) {
  echo "Caught load class failed exception (".$e->getMessage().").";
}
 [2009-11-20 17:56 UTC] jani@php.net
Thank you for not reading the manual or searching existing bug reports. RTFM: http://php.net/autoload

 [2009-11-20 22:40 UTC] krzysztof dot talajko at gmail dot com
Thank you for the Friendly Welcome.

My suggestion is to fix the manual in autoloading section, because it is confusing. In php 5.3 exceptions can be thrown without this error. There is no information about this difference between various php versions. Second confusing thing is that in Zend Framework there is also exception thrown in autoload function ... but I assume that some of ZF programmers just didn't read the manual.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 10:01:30 2025 UTC