Autoloading for fun and profit

So, as I stated in a previous post, the code base here at dealnews is going through some changes. ; I have been working


Autoloading for fun and profit

Posted on: July 27, 2010
So, as I stated in a previous post, the code base here at dealnews is going through some changes. ; I have been working heavily on those changes since then. ; Testing and benchmarking to see what works best. One of those changes is the heavy use of autoloading. ;

During the holidays, I always like to read the PHP Advent. ; One of the posts this year was by Marcel Esser titled You Don’t Need All That. It was a great post and echos many things I have said about PHP and web development. In his post, Marcel benchmarks the difference between using an autoloader and using straight require statements. I was not surprised by the result. The autoloading overhead and class overhead is well known to me. It is one thing that kept me from using any heavy OOP (we banned classes on our user facing pages for a long time) in PHP 4 and PHP 5.0. It has gotten a lot better however. Class overhead is very, very small now. Especially when using it as a namespacing for static functions. However, there is one word of caution I would like to add to his statements. When you use require/include statements instead of autoloading, you end up with a file like this:

require "DB.php";
require "Article.php";


function myfunc1() {
; ; ; ;DB::somemethod();
}

function myfunc2(){
; ; ; ;Article::somemethod();
}

?>
That file needs to require two files, but each one is only needed by one function in the file. ; This is the dilemma we have found ourselves in. ; We have a file that is filled with functions for building, taking apart, repairing, fetching, or anything else you can think to do with a URL. So, at the top of that file are 13 require statements. This all happened organically over time. But, now we are in a situation where we load lots of files that may not even be needed to begin with. By moving to an autloading system, we will only include the code we need. This saves cycles and file IO.

Again, Marcel's post was dead on. Our front end is written with multiple entry points. ; We use auto_prepend files to do any common work that all requests need (sessions, loading the application settings, etc). ; The front page of dealnews.com is about 600 lines of PHP that does the job as quickly as possible. But, we are moving it to use autoloaded code because its require list has grown larger than I would like and some of those requires are not always "required" to generate the page.

One point Marcel did make was about how modern MVC systems are part of the problem. We don't have a highly structured traditional MVC layout of our code. We use a very wide, flat directory structure rather than deep nesting of classes and objects. We also don't do a lot of overloading. Maybe 1% of our classes extend another and never more than one deep. All that makes for much quicker loading objects vs. some of the packaged frameworks like Zend Framework.

So, as Marcel warned, be aware of both your use of autoloading and require statements. Both can be bad when used the wrong way.http://brian.moonspot.net/php-autoloading
Home

TagCloud:                            

Recent Posts

Augmented Reality Coming to DC Bus Stops Today (Photo)
Java - It's not Dead, Folks - It's Doing Just Fine
'NHL2K11' shoots, but sometimes misses
Hack of the Day: Use PHP's List Function to Quickly Transform Arrays to Variables
Web Design Basics - Do You Know the 5 Things Every Web Site Should Not Be Without?
links for 2010-09-04
IBM at the US Open - Analyzing Every Volley, Serve and Overhead Smash
SAP?'????RAC 11g R2 ????
Separating Apache logs by virtualhost with Lua
Web Design For Beginners

Categories


Archives

  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • Sponsors

    Partners

    Programming Tool Links Webmaster tricks and tips Graphic Design Links
    Add Web Tool links Web Design Tool links IT plugin Links


    Sitemap | Google Sitemap | RSS Feeds