Req #69663 [Opn->Fbk]: spl_autoload_error_callback(): registering a callback when all autoloaders fail
| From: | danack@php.net | Date: | Wed, 20 May 2015 10:32:54 +0000 |
| Subject: | Req #69663 [Opn->Fbk]: spl_autoload_error_callback(): registering a callback when all autoloaders fail | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192780@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69663&edit=1
ID: 69663
Updated by: danack@php.net
Reported by: himself at alexanderjank dot de
Summary: spl_autoload_error_callback(): registering a
callback when all autoloaders fail
-Status: Open
+Status: Feedback
Type: Feature/Change Request
Package: SPL related
Operating System: *
PHP Version: 5.6.9
Block user comment: N
Private report: N
New Comment:
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?
Previous Comments:
------------------------------------------------------------------------
[2015-05-19 16:16:33] 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');
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69663&edit=1