I am unable to load the classes using spl_autoload_register

Asked

Viewed 369 times

1

I’m trying to load a class that is in another folder in the core.php file and I’m not getting it

My browser informs this message

Fatal error: Uncaught Error: Class 'Core' not found in /var/www/html/cursophp/superAvancado/mvc/views/index.php:18 Stack trace: #0 {main} thrown in /var/www/html/cursophp/superAvancado/mvc/views/index.php on line 18

Below is the contents of the index.php file

    spl_autoload_register(function($class){
    if (strpos($class, 'Controller') > -1) {
        if (file_exists('controllers/'.$class.'.php')){
            require_once 'controllers/'.$class.'.php';

        }else if(file_exits('models/'.$class.'.php')){
            require_once 'models/'.$class.'.php';

        }else {
            require_once 'core/'.$class.'.php';
        }


    }
});
$core = new Core();

Below is the content of the core.php file.

<?php

class Core {

}

?>
  • 1

    If you are using Linux (obviously), check the name of the folders as it differentiates between case letters and minuscules.

1 answer

0

I found out what the problem actually was, the file name was core.php whereas the class name was Core() that is, the file name was different from the class name, from the moment I put the two as the same name the require_once calling the file started working, if anyone can clarify a little more the answer I appreciate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.