ID:               39313
 Updated by:       [EMAIL PROTECTED]
 Reported By:      justin dot foell at sellingsource dot com
 Status:           Open
 Bug Type:         SPL related
-Operating System: Gentoo Linux
+Operating System: *
-PHP Version:      5.1.6
+PHP Version:      5.2.0
-Assigned To:      
+Assigned To:      helly
 New Comment:

I see your point, will get fixed in 6.0 and in 5.2.1 hopefully.


Previous Comments:
------------------------------------------------------------------------

[2006-10-31 21:00:12] 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 20:51:08] 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 18:43:05] [EMAIL PROTECTED]

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 00:27:44] 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



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=39313&edit=1

Reply via email to