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

1
  • possible duplicate of PHP spl_autoload Commented Jul 21, 2013 at 12:34

1 Answer 1

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 https://www.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 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.