|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 04:00:01 2025 UTC |
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.