autoload php 7.4 code example

Example: autoload.php

The introduction of spl_autoload_register() gave programmers 
the ability to create an autoload chain, 
a series of functions that can be called to try and load a class or interface. 

For example:

<?php
function autoloadModel($className) {
    $filename = "models/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

function autoloadController($className) {
    $filename = "controllers/" . $className . ".php";
    if (is_readable($filename)) {
        require $filename;
    }
}

spl_autoload_register("autoloadModel");
spl_autoload_register("autoloadController");

Tags:

Php Example