Reputation: 1731
I have a file structure that looks like this;
/
/public
-index.php
-login.php
/config.php
/init.php
/classes/ClassGroup/ClassName.class.php
__autoload is defined in config.php, with absolute path to classes. config.php is required in index.php, but when I try to initiate a new class;
$user = new User_User;
Results in;
Fatal error: Class 'User_User' not found in /......./public/index.php on line 27
It does not find it, and when trying to echo out something at the very start of __autoload(), it doesnt do that either, so it seems to me that it does not run the function when not finding a class. Anyone have a clue what the problem might be?
function __autoload($class){
//echo "autoloader started";
$pieces = explode('_', $class);
$path = __SITE_PATH.'/classes';
foreach( $pieces as $i ){
$path .= '/'.$i;
}
//echo "trying to include " .$path.".class.php";
require_once( $path . '.class.php' );
}
Upvotes: 0
Views: 320
Reputation: 1731
I found the problem, apparently this function is not being called automatically. Here is the fix;
spl_autoload_register('__autoload');
This worked.
Upvotes: 2
Reputation: 11492
Your __autoload
function does a require()
, which means it should die with a fatal error if the file is not found. This means that there are two possibilities:
__autoload
function is not being called__autoload
function is loading User.class.php, but that file does not define the class User_UserAdd an echo $path; die;
to your __autoload
to see if it is actually being called and what it's trying to load. Check that User.class.php does actually define the class User_User, and you don't have a typo in the class name.
Upvotes: 1