Disable autoloading and enable it again later
Juli 17th, 2013
For some reasons it might be useful to disable autoloading, do some stuff and enable it again later. When using some frameworks or libraries this should be done in an anonymous way. For that pupose PHP provides the following functions:
spl_autoload_register();
spl_autoload_unregister();
spl_autoload_functions();
The following snippets whill show how to use them:
register some autoload functions:
spl_autoload_register(function($classname) {
echo "trying to autload1 $classname\n";
});
spl_autoload_register(function($classname) {
echo "trying to autload2 $classname\n";
});
get list of all functions:
$functions = spl_autoload_functions();
disable all autoload functions:
foreach($functions as $function) {
spl_autoload_unregister($function);
}
enable them again:
foreach($functions as $function) {
spl_autoload_register($function);
}
When introspection of the autoloader stack is desired this article about dumping the source code of a closure might be interesting as well.
Leave a Reply