php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #31102 Exception not handled when thrown inside __autoload()
Submitted: 2004-12-15 21:07 UTC Modified: 2007-07-21 18:07 UTC
Votes:12
Avg. Score:4.3 ± 0.8
Reproduced:12 of 12 (100.0%)
Same Version:6 (50.0%)
Same OS:6 (50.0%)
From: php_nospam at ramihyn dot sytes dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
45 + 14 = ?
Subscribe to this entry?

 
 [2004-12-15 21:07 UTC] php_nospam at ramihyn dot sytes dot net
Description:
------------
The code below prints a fatal error. This is incorrect as the 
throwing of the exception is supposed to bring us 
immediately to the catch block. Its also inconsistent because when i replace the $bug = new Bug(); by __autoload("Bug"); the exception is handled correctly. Code following the throw is not executed, as expected.

Reproduce code:
---------------
function __autoload($class)
{
	throw new Exception();
}

try
{
	$bug = new Bug(); // Results in a fatal error
	#__autoload("Bug"); // Prints "exception caught."
}
catch (Exception $e)
{
	echo "exception caught.";
}


Expected result:
----------------
exception caught.

Actual result:
--------------
Fatal error: Function __autoload(Bug) threw an exception of type 'Exception' in D:\y\index.php on line 7

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-15 22:41 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

__autoload is the last resort for the engine if it is looking for a class. If it can not be found it is seen as a fatal error (ie \"class not found\") and the script is aborted. This also happens when you throw an exception to signal that the class could not be loaded.
 [2004-12-15 23:38 UTC] php_nospam at ramihyn dot sytes dot net
Correction: this does *not* happen when i throw an exception inside __construct(), in this case its also handled correctly.
 [2004-12-15 23:59 UTC] php_nospam at ramihyn dot sytes dot net
Also notice: the message isn't "Class 'Bug' not found" but an unhandled exception.
 [2004-12-16 12:28 UTC] php_nospam at ramihyn dot sytes dot net
Maybe you should rename the function to __lastresort() ;-)
__autoload() sounds like autorun to me.
 [2004-12-16 12:57 UTC] php_nospam at ramihyn dot sytes dot net
Your statement about that "last resort" is *not* documented.
The Zend Engine 2 changes list says:

The __autoload() interceptor function will be automatically called when an undeclared class is to be instantiated. The name of that class will be passed to the __autoload() interceptor function as its only argument.

What __autoload() does is breaking the Exception handling, so I consider this as a bug.
 [2004-12-16 13:04 UTC] derick@php.net
hehe, perhaps. But we should mention this in the __autoload documentation though.
 [2004-12-29 17:15 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Exceptions thrown in __autoload function cannot be catched in the 'catch' block and result in fatal error."
 [2005-03-21 21:49 UTC] helly@php.net
For further details on exceptions in __autoload() see:
http://cvs.php.net/co.php/ZendEngine2/tests/bug31102.phpt?r=HEAD
 [2005-03-25 18:51 UTC] nlopess@php.net
helly has introduced a way to catch the exceptions, so it needs to be documented. (more details in the file he mentioned)
 [2006-04-12 21:11 UTC] bjori@php.net
I honestly can't think of better example, but I guess it's better than nothing?

http://php.is/bugs/31102/phpdoc.patch.txt
 [2007-07-21 18:07 UTC] nicobn@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

Closed the bug, as it is explicitly stated in the documentation that exceptions thrown into __autoload() cannot be caught.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Oct 23 18:01:30 2019 UTC