Skip to menu

Skip to content



Using an autoload function to make your life easier

Wednesday, October 1st, 2008 at 2:16pm, in Misc, written by Stefan Ashwell

In PHP 5 you can make your life a lot easier by writing an autoload function. This function will automatically load your classes, so you do not need to repeatedly include classes on every page.

It is best to create an autoload function in a file that is included on every page. Typically when I develop a site I like to have a configuration file that I include on every page which does database connections and other site wide operations, so I'll put mine in there.

function __autoload($class) {
   require_once 'classes/'.$class . '.php';
}

What the above function will do is run whenever you create a new object and attempt to include the class file for you, on the fly! So now whenever I need to create a new object I just need to do the following, and the class file will be included for me.

$news = new News();

You can expand this function to be a little more intelligent. Firstly by checking that the class file exists, and secondly you may have more than one directory holding class files. This can be done like so:

function __autoload($class) {
   $classpath = 'classes/'.$class . '.php';
   if ( file_exists($classpath) {
      require_once $classpath;
}
   $classpath = 'libs/'.$class . '.php';
   if ( file_exists($classpath) {
      require_once $classpath;
}

I hope you've found this article useful, if you have any comments don't hesitate to leave one!

Share and Enjoy:

  • bodytext
  • del.icio.us
  • Facebook
  • Google
  • Reddit
  • Technorati
  • Furl
  • StumbleUpon
  • NewsVine
  • Slashdot
  • Spurl
  • Ma.gnolia
  • Live
  • YahooMyWeb
  • E-mail this story to a friend!

Subscribe to Total PHP: RSS | Email

Related Posts:

- How to Read an RSS Feed with PHP 5

- Using a global configuration file

- Choosing a PHP Web Host

- Why you will love PHP5

Comments on this article

No Avatar

By livemaker, Tuesday, October 21st, 2008 at 7:46pm

this is wonderful tutorial .. i read it 3 times and get a fantastic results and sure i put a
copy of this lesson on my site here

www.hmztwsl.com
www.hmztwsl.com/vb

No Avatar

By buck2769, Thursday, October 2nd, 2008 at 6:02pm

to avoid conflicts, try using spl_autoload_register('function_you_want_to_call') instead rather than defining everything in the __autoload method.

dashifen's Avatar

By dashifen, Thursday, October 2nd, 2008 at 2:33pm

Here's my function: http://pastie.org/283636

It's even more complex but handy for sites that have a lot of classes. It will search through a folder and all sub-folders for the specified class file. If it finds the file, then it includes it. This allows me to use multiple folders to group classes based on inheritance, interface implementation, or even just similar functionality (e.g., display of database information). Plus, I can move them around within the default folder and, since the function searches through all folders, it'll still find it.

Further, I make sure that my autoload function skips any folder that begins with a dot. In that way, I can stick third-party classes in the same default folder but not search through them for my own classes. Yes, this means that I have to explicitly include third-party class information but because I cannot guarantee that third-party tools follow my naming conventions, that's often the case.

I'm sure it's far more inefficient than the functions above, but I like it because it trades that inefficiency for greater flexibility.

Please log in to post a comment about this article.


Subscribe to our RSS Feed

Subscribe: RSS | Email

Clear Content - Easy to use PHP 5 Content Management System
CSS Contest

Recommended resources

Recent articles