PHP - spl_autoload_register example

spl_autoload_register automatically loads classes.
Once write autoloader function that parses file path from class namespace and
You will never have to include every php files manually with functions like "require".
    
    // function contains logic of parsing file path from namespace
    function myAutoLoader($className) {
        require str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php';
    }
    
    spl_autoload_register('myAutoLoader');
    
    $db = new \models\db\DbConnection();
    

Comments