1

I have a page called house.php and a class in house.class.php. When I create an instace of that class in house.php it works. Now I want to make a chair.php file and a class Chair that extends House in house.class.php. Where should I put that class? spl_autoload_register doesn't find my class since its name isn't the same with the file. How can I improve function AutoloadClass ? Should I create a chair.class.php and use an require with house.class.php?

spl_autoload_register(null, false);
spl_autoload_extensions('.php, .class.php');
function AutoloadClass($class){
    $filename = $class.'.class.php';

    if (!file_exists($filename))
        {
            return false;
        }
        include $filename;
    }

spl_autoload_register('AutoloadClass');

Thank you

Comy
  • 83
  • 2
  • 9
  • possible duplicate of [PHP spl\_autoload](http://stackoverflow.com/questions/1899073/php-spl-autoload) – tlenss Jul 21 '13 at 12:34

1 Answers1

1

Since you're calling spl_autoload_extensions you don't need to manually add .class.php. Further you can try calling spl_autoload_register with no arguments. It then work with http://de2.php.net/manual/en/function.spl-autoload.php which should work out of the box:

set_include_path(__DIR__);
spl_autoload_extensions('.php,.class.php');
spl_autoload_register();

spl_autoload will do the extension checking. Else you don't need spl_autoload_extensions and have to check for all extensions by yourself.

Another problem that you might face is that your class name is uppercase and your file name is lowercase. I would go with PSR-0 standard so that class names and file names match. So don't add .class.php.

House -> House.php
Chair -> Chair.php
bitWorking
  • 12,485
  • 1
  • 32
  • 38