I am new to PHP. Can any explain how does this code actually works function autoload($class) { require('classes/' . $class . '.class.php'); } // automatically loads all needed classes, when they are needed spl_autoload_register("autoload"); PHP: Thanks
spl_autoload_register registers a callback function to include a class incase we have not included it, so in your code whenever an unregistered class is called PHP interpreter will call the autoload function and then the autoload function will check the classes directory to find a file having classname as its filename, so "new UndefinedClass()" will check for UndefinedClass.php in the classes directory. Please note: You can create a function __autoload() if you don't want to use the spl_autoload_register function and you should be using __autoload() only because spl_autoload_register should be used only when you want to add more than two functions in __autoload stack.