spl_autoload_register does not work

Asked

Viewed 96 times

0

I am developing a system in mvc. Developing locally, everything worked correctly... But when I put it online to be able to test, it just didn’t load, it was 500. So I started trying to find the error. I put echo 'test' inside spl_autoload_register to see if it was entering the function, but it is not. It does not find my core class because it does not enter the spl_autoload_register function :(

I can’t imagine what it might be, if it’s some server setup or something. I’m locally running php 7.4 and online 7.2

Mistake was:

PHP Fatal error: Uncaught Error: Class 'Core' not found in /home/server/public_html/index.php:31 Stack trace: #0 {main} thrown in /home/server/public_html/index.php on line 31

<?php

    session_start();

    // Connection to database
    require('config.php');

    // Checking if the files exist in 'controller', 'core' and 'models'
    spl_autoload_register(function($class) {

        if(file_exists('sources/controllers/'.$class.'.php')) {

            // Controllers
            require 'sources/controllers/'.$class.'.php';

        } else if(file_exists('sources/models/'.$class.'.php')) {

            // Models
            require 'sources/models/'.$class.'.php';

        } else if(file_exists('core/'.$class.'.php')) {

            // Core
            require 'core/'.$class.'.php';

        }

    });

    // Class core
    $core = new Core();

    $core->start();

?>
  • It was the name of the core file that was with minuscule C

  • I’m glad you solved it! But try working with the same version of PHP in both environments!

No answers

Browser other questions tagged

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