Skip to content

Latest commit

 

History

History
58 lines (48 loc) · 1.96 KB

autoload-in-php.md

File metadata and controls

58 lines (48 loc) · 1.96 KB

➲ Autoload in PHP:

It is a mechanism that allows to load multiple other PHP scripts automatically. It is difficult to use explicit declaration of include or require statements every time. It reduces the usages of explicit include or require statements. It is important for large scale applications to load multiple classes, interfaces and so on.

☴ Overview:

  1. Built-in Autoloader
  2. How it works

✦ Built-in Autoloader:

PHP provides a built-in autoloader that can be used to load classes based on their namespace and file paths automatically.

spl_autoload_register(function($classname) {
		$classname .= ".php";
		if (file_exists($classname)) {
    	require $classname;			
		}
});

// Now the class can be available without including the file
$obj = new MyNamespace\MyClass();

To autoload different namespace PHP scripts:

spl_autoload_register(function($classname) {
    $classname = str_replace("\\", "/", $classname); 
    $classname .= ".php";
    if (file_exists($classname)) {
    	require $classname;
    }
});
// Now the class can be available without including the file
$obj = new MyClass();

To autoload different namespace on specific directory:

spl_autoload_register(function ($class) {
    $file = __DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php';
    if (file_exists($file)) {
        require_once $file;
    }
});

✦ How it works:

  1. When try to use a class, PHP checks for the class is already defined.
  2. If the class is missing, then registered autoloader is triggered.
  3. The autoloader searches for a file with specified name as the class in the specified directory.
  4. If the file is found, it adds, making the class available.
  5. If the class is not found throws an error.

⇪ To Top

❮ Previous TopicNext Topic ❯

⌂ Goto Home Page