Question

I am using spl_autoload_register() function to include all files . What i want that any class having extension .class.php or .php would includes directly. I made below class and register two different function and everything working fine, BUT

I think there is some way so that i need to register only one function to include both extensions together.

please have a look on my function and tell me what i am missing

my folder structure

project
      -classes
           -alpha.class.php
           -beta.class.php
           -otherclass.php
      -includes
           - autoload.php
      -config.inc.php // define CLASS_DIR and include 'autoload.php'

autoload.php

var_dump(__DIR__); // 'D:\xampp\htdocs\myproject\includes' 
var_dump(CLASS_DIR); // 'D:/xampp/htdocs/myproject/classes/' 
spl_autoload_register(null, false);
spl_autoload_extensions(".php, .class.php"); // no use for now
/*** class Loader ***/
class AL
{
     public static function autoload($class)
     {
          $filename = strtolower($class) . '.php';
          $filepath = CLASS_DIR.$filename;
          if(is_readable($filepath)){
              include_once $filepath;
          }
//          else {
//                trigger_error("The class file was not found!", E_USER_ERROR);
//            }
    }

    public static function classLoader($class)
    {
        $filename = strtolower($class) . '.class.php';
        $filepath = CLASS_DIR . $filename;
        if(is_readable($filepath)){
              include_once $filepath;
          }
    }

}
spl_autoload_register('AL::autoload');
spl_autoload_register('AL::classLoader');

Note : there is no effect on line spl_autoload_extensions(); . why?

i also read this blog but did not understand how to implement.

Was it helpful?

Solution

There is nothing wrong with the way you do it. Two distinctive autoloaders for two kinds of class files are fine, but I would give them slightly more descriptive names ;)

Note : there is no effect on line spl_autoload_extensions(); . why?

This only affects the builtin-autoloading spl_autoload().

Maybe it's easier to use a single loader after all

 public static function autoload($class)
 {
      if (is_readable(CLASS_DIR.strtolower($class) . '.php')) {
          include_once CLASS_DIR.strtolower($class) . '.php';
      }  else if (is_readable(CLASS_DIR.strtolower($class) . '.class.php')) {
          include_once CLASS_DIR.strtolower($class) . '.class.php';
      }
}

You may also omit the whole class

spl_autoload_register(function($class) {
    if (is_readable(CLASS_DIR.strtolower($class) . '.php')) {
        include_once CLASS_DIR.strtolower($class) . '.php';
    }  else if (is_readable(CLASS_DIR.strtolower($class) . '.class.php')) {
        include_once CLASS_DIR.strtolower($class) . '.class.php';
    }
});

OTHER TIPS

Maybe this will help:

http://php.net/manual/de/function.spl-autoload-extensions.php

Jeremy Cook 03-Sep-2010 06:46

A quick note for anyone using this function to add their own autoload extensions. I found that if I included a space in between the different extensions (i.e. '.php, .class.php') the function would not work. To get it to work I had to remove the spaces between the extensions (ie. '.php,.class.php'). This was tested in PHP 5.3.3 on Windows and I'm using spl_autoload_register() without adding any custom autoload functions.

Hope that helps somebody.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top