php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39313 spl_autoload triggers Fatal error
Submitted: 2006-10-31 00:27 UTC Modified: 2006-11-03 19:07 UTC
From: justin dot foell at sellingsource dot com Assigned: helly (profile)
Status: Closed Package: SPL related
PHP Version: 5.2.0 OS: *
Private report: No CVE-ID: None
 [2006-10-31 00:27 UTC] justin dot foell at sellingsource dot com
Description:
------------
If the default spl_autoload function is at the top of the stack and the class is not found, a Fatal error is triggered.  I would think you would want to fail silently, that way the fast C code can be used first, then fall-back to your custom PHP Code.  Then if the class is still not found, let PHP deal with it.

The following code will work if the two spl_autoload_register lines are switched.

Reproduce code:
---------------
<?php
//MyClass.php
class MyClass
{
	public function __construct()
	{
		echo __CLASS__, "\n";
	}
}
?>

<?php
//test.php
function myloader($class_name)
{
	return @include_once($class_name . ".php");
}

spl_autoload_register('spl_autoload');
spl_autoload_register('myloader');
print_r(spl_autoload_functions());
$myclass = new MyClass();
?>

Expected result:
----------------
Array
(
    [0] => spl_autoload
    [1] => myloader
)
MyClass

Actual result:
--------------
Array
(
    [0] => spl_autoload
    [1] => myloader
)

Fatal error: Class 'MyClass' not found in /virtualhosts/justin/test/test.php on line 12


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-31 18:43 UTC] helly@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

The order is respected as you described. But if you are using a non existing class the __autoload facility starts its work. And that means spl\'s autoload stuff is being invoked. That then calls all registered autoload functions one after another. And only if all fail you get an error, where the error is generated from the spl autoload handling system and send to the engine\'s autoload facility.
 [2006-10-31 20:51 UTC] justin dot foell at sellingsource dot com
Maybe your explanation and/or the SPL autoload documentation is unclear... but if I have two functions registered with the SPL autoloader, the default 'spl_autoload' being the first and my own being the second, the class will not be loaded even though the seconds autoloader would normally find it (and will find it if it's first in the stack).  So it seems it is failing not if _NO_ autoloaders find the class, but if the first 'spl_autoload' method does not find the class.  This has been tested on PHP (cli) 5.1.6 on both Gentoo and Ubuntu as well as with the php5.2-200610311730 snapshot.

The issue is that the spl_autoload C code throws and exception if the class is not found:
//php_spl.c - line 310:

    if (!found) {
        zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Class %s could not be loaded", class_name);
    }

This stops further excecution of any autoloaders.  When commented out, it works as I would expect it to, failing silently then continuing to the next registered loader.
 [2006-10-31 21:00 UTC] justin dot foell at sellingsource dot com
This may possibly be due to the fact that the Zend engine will turn any thrown exceptions in the __autoload method into Fatal errors.
 [2006-10-31 23:16 UTC] helly@php.net
I see your point, will get fixed in 6.0 and in 5.2.1 hopefully.
 [2006-11-03 19:07 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC