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

spl_autoload

Description

The spl_autoload of SPL for PHP default implementation for __autoload().

Syntax

spl_autoload(
    string $class,
    ?string $file_extensions = null
): void

Parameters

class

The name of the class (and namespace) being instantiated.

file_extensions

By default it checks all include paths to contain filenames built up by the lowercase class name appended by the filename extensions .inc and .php.

Return

No value is returned.

Examples

1 · class

<?

function callback($class)
{
    spl_autoload($class);
}

$callback = "callback";

spl_autoload_register($callback);

?>

2 · file_extensions

<?

function callback($class)
{
    $file_extensions = ".php,.inc";

    spl_autoload($class, $file_extensions);
}

$callback = "callback";

spl_autoload_register($callback);

?>

3 · namespace · class

<?

namespace mynamespace;

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

        spl_autoload_register($callback);
    }
    public function callback($class)
    {
        spl_autoload($class);
    }
}

new myclass();

?>

4 · namespace · file_extensions

<?

namespace mynamespace;

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

        spl_autoload_register($callback);
    }
    public function callback($class)
    {
        $file_extensions = ".php,.inc";

        spl_autoload($class, $file_extensions);
    }
}

new myclass();

?>
HomeMenu