|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69663 spl_autoload_error_callback(): registering a callback when all autoloaders fail
Submitted: 2015-05-19 16:16 UTC Modified: 2015-05-20 10:53 UTC
From: himself at alexanderjank dot de Assigned:
Status: Closed Package: SPL related
PHP Version: 5.6.9 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
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:
Solve the problem:
45 + 6 = ?
Subscribe to this entry?

 [2015-05-19 16:16 UTC] himself at alexanderjank dot de
Today I want to request the feature of having a function callback (callable) in case all autoloaders on the SPL-Autoloader-Stack failed.
Personally, I'll find it useful, to trow an Exception when all the autoloaders weren't able to load the class. For me a workaround is, the register a last SPL-Autoload function, in which I throw the Exception (the autoloaders before my last wren't successful, if my function runs). But I don't thing, that this is a semantically correct way to do this. In my opinion, something like a error callback should exist.

Greetings from Germany,
Alexander Jank

Test script:
class ClassNotFoundException extends \RuntimeException{
	public function __construct($className) {
			'Class "%s" does not exist',
function MainAutoloader($class) {
	//look for the class and include it (if file exists)
	if(class_exists($class)) {
		return true;
	} else {
		return false; /* next autoloader will be called */
function MainAutoloadErrorHandler($class) {
	throw new ClassNotFoundException($class);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-19 20:51 UTC]
-Summary: spl_autoload_error_callback(): registering a cllaback when all autoloaders fail +Summary: spl_autoload_error_callback(): registering a callback when all autoloaders fail -Operating System: Linux & Windows +Operating System: *
 [2015-05-20 10:32 UTC]
-Status: Open +Status: Feedback
 [2015-05-20 10:32 UTC]
In PHP 7, trying to instantiate a class that is not present will throw an exception: 

try {
    $foo = new Bar();
catch(\EngineException $e) {
    echo "Missing class ".$e->getMessage();

(the actual name of the exception name may change).

Presumably your workaround will tide you over until that's released?
 [2015-05-20 10:53 UTC] himself at alexanderjank dot de
-Status: Feedback +Status: Closed
 [2015-05-20 10:53 UTC] himself at alexanderjank dot de
Yes, I think, that would be okay like this, until PHP 7. 
I just want to say, that for me it would be good, if this Exception would have a good name, where you can see directly, that there was an error in including that class.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Jan 22 13:03:41 2022 UTC