Page 1 of 1

__autoload issues

Posted: Thu Jan 17, 2008 4:26 pm
by gormster
Okay, so I have a lot of classes I want to autoload. I've got a preferences file which has class names associated with file names, and I've read, at the beginning of my page, that file into an assoc. array named $classes (as class => file). Now, I thought that my __autoload would be pretty simple; it looks like this:

Code: Select all

function __autoload($class_name)
{
  require_once(SNOW_CLASS_PATH . $classes[$class_name]);
}
Unfortunately this doesn't work. (SNOW_CLASS_PATH, by the way, is the path to the classes folder.) I added a var_dump on $classes in the function, and it gave me a NULL. A little research pointed out that __autoload happens outside any scope, so I guess it can't access any of the variables on the page. Does anyone have a solution? Note that globaling the $classes variable isn't an option since I'm going to be loading classes dynamically.

Appreciate your help
-gorman

Re: __autoload issues

Posted: Thu Jan 17, 2008 4:30 pm
by Christopher
You either need to declare $classes global or move the array into __autoload. The latter is probably preferable.