Search code examples
phpautoloader

Temporarily disable autoload stack


Can someone please tell me if it is possible to temporarily disable the spl_autoloader and then enable it later, and if so how? Alternatively, if that is not possible, is there a way to 'pull' the autoload stack into a temporary variable, which causes the stack to deactivate (when its empty) and then re-register everything from that variable.


Solution

  • Credit to Bart who had the solution

    you can use spl_autoload_functions to collect the functions and then unregister them with spl_autoload_unregister

    and I tested with the following which worked:

    <?php
    
    $autoloadFuncs = spl_autoload_functions();
    var_dump($autoloadFuncs);
    
    foreach($autoloadFuncs as $unregisterFunc)
    {
        spl_autoload_unregister($unregisterFunc);
    }
    
    // Code goes here that you dont want the auto loader enabled for.
    
    foreach($autoloadFuncs as $registerFunc)
    {
        spl_autoload_register($registerFunc);
    }
    ?>