php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #50250 Exceptions can be thrown and caught in an autoload function
Submitted: 2009-11-20 19:50 UTC Modified: 2009-11-25 11:00 UTC
From: brandon@php.net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.3.1 OS: *
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: brandon@php.net
New email:
PHP Version: OS:

 

 [2009-11-20 19:50 UTC] brandon@php.net
Description:
------------
According to the PHP documentation, exceptions cannot be thrown inside __autoload() functions, and prior to PHP 5.3, exceptions thrown resulted in fatal errors. Since PHP 5.3, it has been possible to throw and catch exceptions from __autoload() functions.

I'm happy to chalk this up to a documentation bug, and if it is I will gladly fix it myself; however, I want to make sure this is intended functionality and not an accidental inclusion that might be fixed in future versions of PHP.

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

function autoload($class)
{

throw new Exception('test');
require $class . '.php';

}

spl_autoload_register('autoload');

try {
	new NoExist();
} 
catch (Exception $e)
{
	echo 'unable to load the class.';
}

Expected result:
----------------
Fatal error, as the autoloader throws an exception which is not allowed.

Actual result:
--------------
string of "unable to load the class."

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-20 20:14 UTC] bjori@php.net
This is also true for __autoload(), not only spl_register_autoload().

Bug or expected behavior as of PHP 5.3.0?

 [2009-11-23 11:36 UTC] rquadling@php.net
One instance where the documentation does hold true is attempting to 
throw an exception of a type which is not available.

Changing ...

throw new Exception('test');

to ...

throw new MissingException('test');

results in ...

Fatal error: Class 'MissingException' not found in Z:\testauto.php on 
line 6


I wonder if this is what the documentation is relating to.
 [2009-11-23 11:55 UTC] rquadling@php.net
Amendeded the code slightly ...

<?php
echo "\n", PHP_VERSION;

if (function_exists('spl_autoload_register')) {
	function autoload($class) {
		throw new Exception('test');
		require $class . '.php';
	}
	spl_autoload_register('autoload');
} else {
	function __autoload($class) {
		throw new Exception('test');
		require $class . '.php';
	}
}

try {
	new NoExist();
} 
catch (Exception $e) {
	echo "unable to load the class.\n";
}


5.0.0
Fatal error: __autoload(NoExist) threw an exception of type 
'Exception' in Z:\testauto.php on line 29

5.0.1 - 5.0.5
Fatal error: Function __autoload(NoExist) threw an exception of type 
'Exception' in Z:\testauto.php on line 29

5.1.0 - 5.2.11
Fatal error: Class 'NoExist' not found in Z:\testauto.php on line 29

5.3.0 - 5.3.1
unable to load the class.


And using MissingException

5.0.0 - 5.0-5
Fatal error: Class 'MissingException' not found in Z:\testauto.php on 
line 12

5.1.0 - 5.3.1
Fatal error: Class 'MissingException' not found in Z:\testauto.php on 
line 6


 [2009-11-24 20:22 UTC] jani@php.net
So what is this report about? Are you really complaining because something WORKS? If so, please change this to documentation issue.
 [2009-11-24 21:08 UTC] brandon@php.net
This report is about the fact that the PHP team closed a bug as "wontfix" when someone complained that exceptions raised in autoloaders could not be caught.

See http://bugs.php.net/bug.php?id=26193

Thus, there is an inconsistency in PHP, and one that should be resolved to ensure that this isn't deemed a hole later on that must be "fixed," breaking code for those that thought this was by design.
 [2009-11-25 07:37 UTC] jani@php.net
So now it's working, document it.
 [2009-11-25 11:00 UTC] svn@php.net
Automatic comment from SVN on behalf of rquadling
Revision: http://svn.php.net/viewvc/?view=revision&revision=291300
Log: Autoload and exceptions work. Mostly. Fix#50250
 [2009-11-25 11:00 UTC] rquadling@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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 01 01:01:28 2024 UTC