On PHP.net the description for __autoload reads “Attempt to load undefined class”. However, it states you can define the function “to enable classes autoloading.” So, say I define the function like this:
function __autoload($className) {
if (file_exists(ROOTDIRECTORY . $className . '.class.php')) {
require_once(ROOTDIRECTORY . $className . '.class.php');
}
}
Will __autoload still only load classes that haven’t been defined yet, or will the guts of the function as they are written above override this feature?
Yes, it will only trigger for classes that have yet to be included. This means you can replace
require_once()with a simplerrequire()which reduces the amount of overhead processing PHP has to do in the background.Also, you should add an
else { return false; }to your function so that PHP triggers the appropriate error if it cannot locate the class file.