Search code examples
phpspl-autoloader

HTML Error 500 when I including autoloader


This is my code. Index.php

define('_PATH', __DIR__ . '/');
require_once('libs/classloader.php');
echo 'test';

Classloader.php

function ClassLoader($className)
  {
    if(file_exists(__DIR__ '/class.'. strtolower($className) . '.php'))
    {
      require_once(__DIR__ '/class.'. strtolower($className) . '.php');
    }
    else {
      echo 'ERROR: '. $className;
    }
  }

  spl_autoload_register('ClassLoader');

I see only error 500 in my browser. PHP version is 5.4 and server is LiteSpeed.


Solution

  • I think this small change should helps:

    function ClassLoader($className)
    {
        if(file_exists(__DIR__ .'/class.'. strtolower($className) . '.php'))
        //if(file_exists(__DIR__ '/class.'. strtolower($className) . '.php'))
        {
          require_once(__DIR__ .'/class.'. strtolower($className) . '.php');
          //require_once(__DIR__ '/class.'. strtolower($className) . '.php');
        }
        else {
          echo 'ERROR: '. $className;
        }
    }
    
    spl_autoload_register('ClassLoader');