Autoloading headache
Posted: Fri Nov 12, 2010 8:40 am
I stumbled on something regarding the PHP autoloader. If I have the following in a file called A.php:
and then in ThirdClass.php I have:
I get a PHP Warning: include(): Failed opening 'SubClass.php' for inclusion and PHP Fatal error: Class 'SubClass' not found. If SubClass doesn't extend ThirdClass I get no error. It seems that using the autoloader requires that subclasses be declared after parent classes if they're declared in the same file, something which is not required if not using the autoloader. Can someone confirm and possibly explain this?
Code: Select all
function __autoload($class_name) {
include $class_name . '.php';
}
class Starter extends SubClass {
function __construct() {
parent::__construct();
print "In Starter constructor\n";
}
}
class SubClass extends ThirdClass {
function __construct() {
print "In SubClass constructor\n";
}
}
$obj = new Starter();
Code: Select all
class ThirdClass {
function __construct() {
print "In ThirdClass constructor\n";
}
}