spl_autoload_call

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

spl_autoload_callTry all registered __autoload() functions to load the requested class

Beschreibung

spl_autoload_call(string $class): void

This function can be used to manually search for a class, interface, trait, or enumeration using the registered __autoload functions.

Parameter-Liste

class

The class name being searched.

Rückgabewerte

Es wird kein Wert zurückgegeben.

add a note

User Contributed Notes 2 notes

up
-2
Melnofil
4 years ago
A complete example with namespaces:fruits/pinapple.php<?phpnamespace Fruits;echo "pinapple\n";class Pinapple { }?>fruits/pinapple.php<?phpnamespace Vegetables;use Fruits\Pinapple;echo "carrot\n";class Carrot { }new Pinapple(); // Let's call autoload here?>index.php<?phpspl_autoload_register(function($class_name) {    @include_once(__DIR__ . '/' . strtolower(str_replace('\\', '/', $class_name)) . '.php');});new Vegetables\Carrot();?>Result:carrotpinappleindex2.php<?phpspl_autoload_register(function($class_name) {    @include_once(__DIR__ . '/' . strtolower(str_replace('\\', '/', $class_name)) . '.php');});spl_autoload_call('Fruits\\Pinapple'); // Reverse the load orderspl_autoload_call('Fruits\\Pinapple'); // Multiple call is safe with include_oncenew Vegetables\Carrot();?>Result:pinapplecarrot
up
-4
k dot varmark at gmail dot com
14 years ago
It should be noted, that calling spl_autoload_call on a child class, and then on its parent class, throws a fatal error.This happens because autoloading the child class also loads the class it extends. And since spl_autoload_call forcibly calls the registered autoload function(s), not taking into account whether the class exists, a fatal error is thrown:File: child.class.php<?phpclass Child extends Parent () {    public function __construct () {        parent::__construct();    }}?>File: parent.class.php<?phpclass Parent () {    public function __construct () {    }}?>File: autoload.php<?php/*    works fine    */    spl_autoload_call('Child');/*    throws: Fatal error: Cannot redeclare class Parent in /parent.class.php on line 2    */    spl_autoload_call('Parent');?>
To Top