PHP Class Autoload

It’s been a long time since I posted.  I just wanted to write up a quick post today to say how much I like the new __autoload() function in PHP.  For those not familiar with it, the function gets called when a class is used in PHP that has not been defined.  This gives the developer a chance to allow the script to find the include file it needs and include it to define the class.

The function’s not all that new, but I’m just getting around to using it and it’s changing the way I design my sites.

So now I don’t use ever need to manually include a file anymore.  I don’t use any global functions.  I make all my otherwise-global functions static functions of some class and call them that way.  I never have to remember to include the file at the top of the page, I never have to worry about including it twice because of the quirks of include_once(), and I don’t have a bunch of orphaned include()s at the top of the script anymore from removing a function but leaving it’s include.

I’m sure there’s someone out there that thinks this isn’t a good idea for whatever reason.  I’m all ears, but for now, this has made my PHP development much easier.

3 Responses to “PHP Class Autoload”

  1. melissa eimers Says:

    I liked the idea, but had already solved that problem in PHP 4: I included a single file, called “include.php”, which had all the include statements I needed throughout my site, whether this particular page needed them all or not. Anyway, I tried the __autoload function instead of the “all-include” file.

    function __autoload($class_name)
    {
    include_once(’classes/’ . $class_name . ‘.php’);
    }

    Straight away, I hit the problem of calling my php files with all the same name convention. I usually call my classes class.name.php. So In this case I would have to modify the function to include the class. in the front of it. But I don’t only include classes in my scripts! I also use functions, and some classes that would not be found in this folder (like the Smarty class). So my decision is to stay away from the __autoload function. Just using an all-inclusive include.php file works much better for me.

    Just thought I’d say that since no one else has had much to say.

  2. Including all your files in one central include file might have its benefits, such as allowing more flexibility in naming classes and where class files are stored, and compatibility with PHP4. However, I would think that there could be a large drawback to this in terms of performance for large sites. It could also have a potential impact on distributed development.

    I get around the issue of having external classes by either using a stub include file for the external classes, or searching multiple folders for classes. And I find that enforcing a naming convention (any naming convention that you can stick to) makes life easier in the long run.

    Not all who read this will get the inside joke, which is that my sister, the author of the comment, to which I’m replying, is not a programmer and does not, to my knowledge, know any PHP. I was not surprised to see she commented, because she has a reputation for clever humor in comments. But I was quite surprised that her response made sense. I’m flattered that she went to lengths to find an appropriate response even if she did plagiarize it.  Thanks, sis.
    http://www.weberblogs.com/article.php?story=20041129042029399

  3. melissa eimers Says:

    check out my blog: eimers.blogspot.com

Leave a Reply

Not logged in: Log in