php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42823 spl_autoload_register() stack order
Submitted: 2007-10-02 05:10 UTC Modified: 2008-05-25 12:24 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: stanlemon at mac dot com Assigned: colder (profile)
Status: Closed Package: SPL related
PHP Version: 5.2.* OS: *
Private report: No CVE-ID: None
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:
7 + 14 = ?
Subscribe to this entry?

 
 [2007-10-02 05:10 UTC] stanlemon at mac dot com
Description:
------------
spl_autoload_register() builds the autoload stack based upon the 
sequence in which methods are added to the stack.  This gives precedence 
to the first methods loaded into the stack, not the subsequent ones.  
This can be an issue when someone creates an initial autoloader to cover 
the most generic of situations and is designed to error out when nothing 
can be found, either by using require() or by throwing an exception.  If 
I wanted to add a secondary autoloader to account for an additional set 
of classes I would need to either unregister the initial autoloader and 
account for those scenarios or change the order of the autoload stack.

I realize this suggest an API change, which is obviously less than 
desirable.  Perhaps, though, a method like spl_autoload_preregister() 
could be added to prepend autoloaders to the stack, rather than append, 
thus giving them precedence in the call order.

Reproduce code:
---------------
function genericAutoload($class) {
    if (!include($class . '.php')) {
        throw new Exception("Can't include!");
    } else {
        return true;
    }
}

function secondaryAutoload($class) {
    include('library/' . $class . '.php');
}

spl_autoload_register('genericAutoload');
spl_autoload_register('secondaryAutoload');


Expected result:
----------------
Autoloaders added later on in the code would be called first, so the 
stack would be called in the reverse order it is now, or new autoloaders 
would be prepended to the autoloader stack.  This would allow the first 
initial autoloader to error out as the last catch-all, but subsequent 
autoloaders to accomodate particular scenarios, where they would most 
likely not produce a total failure.  The reproduce code will show an 
instance where the secondary autoloader will never be called because the 
first autoloaderis called and designed to fail if a file cannot be found 
for the given class.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-25 12:24 UTC] colder@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.

proto bool spl_autoload_register([mixed autoload_function = "spl_autoload" [, throw = true [, prepend = false]]])
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC