gormster Posted January 17, 2008 Share Posted January 17, 2008 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: <?php 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 Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/ Share on other sites More sharing options...
emehrkay Posted January 17, 2008 Share Posted January 17, 2008 well, is the $classes array global? Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442225 Share on other sites More sharing options...
KrisNz Posted January 17, 2008 Share Posted January 17, 2008 If I understand correctly, you've got a file that has a hash of class names and their associated file name? Doesn't this mean that every time you write a class you have to go and update that file? To me that somewhat defeats the purpose of having autoload. If you use a naming convention for your classes and their associated filenames you can avoid this altogether. If you camelcase your class names e.g <?php class ThisIsMyClass { //.... } ?> Then name the file this_is_my_class.php You can convert the camelcase to the underscored version with this <?php function camelToUnderscore($s) { //http://www.regular-expressions.info/refadv.html //this translates to //look behind(to the right of) the first character for any upper case //character and replace it with an underscore + the matched character return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $s)); } ?> That way you can convert $class_name to its matching file name without needing to keep a reference of it. Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442230 Share on other sites More sharing options...
gormster Posted January 17, 2008 Author Share Posted January 17, 2008 Okay, the thing is this is basically a framework. Developers add their own classes which conform to an interface. I don't want to limit them to a specific naming convention, particularly as they might have one they already like. That's why I'm using the prefs file/$classes hash. I think I can get around it, though, by just not using __autoload... at the moment the classes are only instantiated once, so I'm just going to add a require_once at that point. I'd still like to do it with __autoload though if anyone has any other ideas. Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442243 Share on other sites More sharing options...
KrisNz Posted January 17, 2008 Share Posted January 17, 2008 I don't really understand this.. Note that globaling the $classes variable isn't an option since I'm going to be loading classes dynamically. But you might like to investigate the Registry pattern - this can be used to hold or retrieve a reference to your $classes hash. Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442252 Share on other sites More sharing options...
deadimp Posted January 18, 2008 Share Posted January 18, 2008 I'm also kinda confused why it can't be global... Global data isn't constant (unless you explicitly define it as such), so you can change it. This auto-include system seems like it'd take a little more maintainence than the developer normally included the file or conformed to naming conventions. Then again, centralizing the information would help when you reorganize and you don't have to comb through your files again and change your includes. Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442409 Share on other sites More sharing options...
448191 Posted January 18, 2008 Share Posted January 18, 2008 Stop hurting your brains and use classname <> filename mapping. Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442480 Share on other sites More sharing options...
Daniel0 Posted January 18, 2008 Share Posted January 18, 2008 I do this: <?php function __autoload($class_name) { require str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442718 Share on other sites More sharing options...
448191 Posted January 18, 2008 Share Posted January 18, 2008 That's the classic example: Pear naming convention. Anything goes though (though I do recommend the Pear naming convention). Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442861 Share on other sites More sharing options...
Daniel0 Posted January 18, 2008 Share Posted January 18, 2008 Ah, didn't realize it was that. I haven't really used any pear libraries, but it's the same naming convention in Zend Framework which is the one I find best for PHP. Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442887 Share on other sites More sharing options...
448191 Posted January 18, 2008 Share Posted January 18, 2008 [...] but it's the same naming convention in Zend Framework [...] Yes it is. It's that good. Quote Link to comment https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442926 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.