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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
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.