Home » Php » autoloader – PHP script can't work with autoloaded classes

autoloader – PHP script can't work with autoloaded classes

Posted by: admin February 25, 2020 Leave a comment

Questions:

The UsersView class object can’t be loaded with the custom autoloader, finishing the PHP script with the following error. What would be the solution?

Fatal error: Uncaught Error: Class 'UsersView' not found in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php:4 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/solent/common/registration/pendings.php on line 4

Project structure:

classes
   |_____ UsersView.php

common
   |_____ autoloader.php
   |_____ registration
               |_____ pendings.php

Pendings.php script

include("../autoloader.php");

$pendingsView = new UsersView();
$rows = $pendingsView->getAllUsers();

Autoloader.php script

spl_autoload_register('myAutoLoader');

    function myAutoLoader($className) {
        $path = "../classes";
        $extension = ".php";

        $fullPath = $path . $className . $extension;

        if(!file_exists($fullPath)) {
            return false;
        }

    include_once $fullPath;
How to&Answers: