Jump to content

[SOLVED] Simulating the __autoload() function in PHP4


purpleshadez

Recommended Posts

I have been thinking about a method of simulating the __autoload() function that will work in PHP4.

 

Currently I'm using something like this:

foreach(glob($path . '*.php') as $class_name)
{
  if( ! preg_match("/index.php/i", $class_name)) {
    require_once($class_name);
  }
}

 

As expected this will included all files with with .php extension excluding index.php from the specified file path. Now with a small set of classes I can't really see this as a problem especially if almost all of them are core classes required for the application to work. However, in time I imagine there will be more and more classes that will not be used on a regular basis such as excel exports or generating word documents that really do not need to be loaded on each and every page.

 

Would it be better to perhaps use the above method with a folder path that only contains the core classes and then keep the others in a seperate location and manually include them when needed or is there another way to only load those that are needed?

That's hardly a simulation of autoloading. It just loads all files in given location, while autoloading tries to load classes on demand.

 

Why are you even doing anything in PHP4?

I am aware it doesn't do what autoload does, I was after a better method that would work in a more efficient way than the example I provided.

 

As for why, a number of business processes prevent me from being able to upgrade to the latest version.

I doubt you can do anything more in PHP4 except perhaps what you mentioned in your first post. Either storing different class 'families' in different folders, or including 'family name' in their filename (like 'excel.reports.php')

 

And then calling function like

autoload('excel');  //loads all excel classes

I doubt you can do anything more in PHP4 except perhaps what you mentioned in your first post. Either storing different class 'families' in different folders, or including 'family name' in their filename (like 'excel.reports.php')

 

And then calling function like

autoload('excel');  //loads all excel classes

I like the family name idea, just prefix the class & filenames and call them when needed. Not perfect, but better than my current solution. I could beat the sys admin to death with his keyboard then do the update myself.... :)

 

Cheers Mchl I think I'll go with that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.