php.net |  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
 [2015-05-19 16:16 UTC] himself at alexanderjank dot de
Description:
------------
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:
---------------
<?php
class ClassNotFoundException extends \RuntimeException{
	public function __construct($className) {
		parent::__construct(sprintf(
			'Class "%s" does not exist',
			$className
		));
	}
}
//Workaround
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);
}
spl_autoload_register('MainAutoloader');
spl_autoload_register('MainAutoloadErrorHandler');
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-19 20:51 UTC] cmb@php.net
-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] danack@php.net
-Status: Open +Status: Feedback
 [2015-05-20 10:32 UTC] danack@php.net
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-2021 The PHP Group
All rights reserved.
Last updated: Tue Nov 30 00:03:14 2021 UTC