Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/86543-__autoload-issues/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442230
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442243
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442252
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/86543-__autoload-issues/#findComment-442409
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.