Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

spl_autoload_register

Description

The spl_autoload_register of SPL for PHP registers the given function as __autoload() implementation.

Syntax

spl_autoload_register(
    ?callable $callback = null,
    bool $throw = true,
    bool $prepend = false
): bool

Parameters

callback

The autoload function being registered. If null, then the default implementation of spl_autoload() will be registered.

callback(
    string $class
): void
class

The name of the class. It does not contain the leading backslash of a fully-qualified identifier.

throw

This parameter specifies whether spl_autoload_register() should throw exceptions when the callback cannot be registered.

WARNING: This parameter is ignored as of PHP 8.0.0, and a notice will be emitted if set to false. spl_autoload_register() will now always throw a TypeError on invalid arguments.

prepend

If true, spl_autoload_register() will prepend the autoloader on the autoload queue instead of appending it.

Return

Returns true on success or false on failure.

Examples

1 · void

<?

$return = spl_autoload_register();

var_export($return);

?>
true

2 · callback

<?

function callback($class)
{
    include_once __DIR__ . "/" . $class . ".php";
}

$callback = "callback";

$return = spl_autoload_register($callback);

var_export($return);

?>
true

3 · throw

<?

function callback($class)
{
}

$callback = "callback";
$throw = true;

$return = spl_autoload_register($callback, $throw);

var_export($return);

?>
true

4 · prepend · false

<?

function callback1($class)
{
}
function callback2($class)
{
}
function callback3($class)
{
}

$callback1 = "callback1";
$callback2 = "callback2";
$callback3 = "callback3";
$throw = true;
$prepend = false;

$return1 = spl_autoload_register($callback1, $throw, $prepend);
$return2 = spl_autoload_register($callback2, $throw, $prepend);
$return3 = spl_autoload_register($callback3, $throw, $prepend);

var_dump($return1, $return2, $return3);

$spl_autoload_functions = spl_autoload_functions();

print_r($spl_autoload_functions);

?>
bool(true)
bool(true)
bool(true)
Array
(
    [0] => callback1
    [1] => callback2
    [2] => callback3
)

5 · prepend · true

<?

function callback1($class)
{
}
function callback2($class)
{
}
function callback3($class)
{
}

$callback1 = "callback1";
$callback2 = "callback2";
$callback3 = "callback3";
$throw = true;
$prepend = true;

$return1 = spl_autoload_register($callback1, $throw, $prepend);
$return2 = spl_autoload_register($callback2, $throw, $prepend);
$return3 = spl_autoload_register($callback3, $throw, $prepend);

var_dump($return1, $return2, $return3);

$spl_autoload_functions = spl_autoload_functions();

print_r($spl_autoload_functions);

?>
bool(true)
bool(true)
bool(true)
Array
(
    [0] => callback3
    [1] => callback2
    [2] => callback1
)

6 · namespace

<?

namespace mynamespace;

class myclass
{
    public static function callback($class)
    {
    }
}

$callback = "mynamespace\myclass::callback";

$return = spl_autoload_register($callback);

var_export($return);

?>
true

7 · namespace

<?

namespace mynamespace;

class myclass
{
    public function __construct()
    {
        $callback = __CLASS__ . "::callback";

        $return = spl_autoload_register($callback);

        var_export($return);
    }
    public function callback($class)
    {
    }
}

new myclass();

?>
true
HomeMenu