Req #69663 [Fbk->Csd]: spl_autoload_error_callback(): registering a callback when all autoloaders fail
| From: | himself at alexanderjank dot de | Date: | Wed, 20 May 2015 10:53:35 +0000 |
| Subject: | Req #69663 [Fbk->Csd]: 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-192781@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
User updated by: himself at alexanderjank dot de
Reported by: himself at alexanderjank dot de
Summary: spl_autoload_error_callback(): registering a
callback when all autoloaders fail
-Status: Feedback
+Status: Closed
Type: Feature/Change Request
Package: SPL related
Operating System: *
PHP Version: 5.6.9
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2015-05-20 10:32:52] 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-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