The spl_autoload_unregister Function

The spl_autoload_unregister function allows you to remove a previously registered autoload function from the SPL stack. It accepts a callback function as a parameter, which needs to be removed from the list of autoloaders.

Syntax

phpspl_autoload_unregister(callable $autoload_function);

Example

Let's register and then remove an autoloader:

php<?php
  function my_autoload($class) {
    include 'classes/' . $class . '.php';
  }

  spl_autoload_register('my_autoload');
  spl_autoload_unregister('my_autoload');
?>

Now the my_autoload function will no longer be called when attempting to autoload an undefined class.

Example

Checking the success of removing an autoloader:

php<?php
  function autoload_one($class) {
    echo "Trying to load $class\n";
  }

  spl_autoload_register('autoload_one');
  $res = spl_autoload_unregister('autoload_one');
  var_dump($res);
?>

Code execution result:

phptrue

Example

Attempting to remove a non-existent autoloader:

php<?php
  $res = spl_autoload_unregister('nonexistent_function');
  var_dump($res);
?>

Code execution result:

phpfalse

See Also