__autoload issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gormster
Forum Newbie
Posts: 1
Joined: Thu Jan 17, 2008 4:23 pm

__autoload issues

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: __autoload issues

Post by Christopher »

You either need to declare $classes global or move the array into __autoload. The latter is probably preferable.
(#10850)
Post Reply