Posted by: phillipnb | September 11, 2010

Use of __autoload()

It is not that there aren’t many write-ups and articles about the use of __autoload() on the Internet but certain specific issues are not pointed out in any of these articles. In fact, this article is for all those who are afraid to use ‘__autoload()’. The __autoload function started with PHP’s 5.x release and since then it has become bit famous and notorious. One can find a number of posts on many PHP discussion boards about people whole heartedly supporting __autoload() while there are plenty of others who oppose this functionality.

So, what is this __autoload() all about?. In the early days of PHP and before PHP moved towards object oriented programming, the use of __autoload() was not that necessary. If a developer wanted to include one or more files within the current php code that he was writing, all that he had to do was to use either ‘include’ or ‘require’ statements. But when the PHP applications started getting complex with the extensive use of ‘classes’, developers began to write a single class in a single file. Thus, when a medium sized PHP application was completed, the number of class files ran into several hundreds. Including all these files in the main file using several ‘include’ or ‘require’ statements became a lot of work and confusing. It is here the use of ‘__autoload()’ comes into picture. There are several advantages of using ‘__autoload()’ but a few important things that comes to my mind are:

  • If you use __autoload(), then you do not have to manually load classes before using them
  • You do not have to hard-code class locations
  • You do not have to include classes in a certain order. (For e.g.While using the ‘include’ statement,if classA extends classB, then classB needs to be included before including classA)

I’ll demonstrate what I am talking about by using an exmaple:

Before we had __autoload() function, in order to include five class files, we had to do the following:


This type of lengthy usage of ‘include’ and ‘require’ statements are not necessary if we are to use __autoload(). Here is how the above files can be included using __autoload().

function __autoload($className)

or, you can be a little more accurate by checking the availability of the file to be included using the following code:

function __autoload($className)
  if (file_exists($className.'.php'))

Once you have replaced your include/require statements with the above __autoload(), PHP will load it automatically as soon as it encounters statements like,

$cObj = new ClassB;

When PHP encounters the above statement, PHP will check if ClassB has already been loaded, if not, it will search for ClassB and then loads it. It will be wise not to use __autoload() if you do not like ‘PEAR like’ directory/file naming convention. For e.g. the file usr_class_student.php will get translated to usr/class/student.php. In this case, just replace all ‘_’ in usr_class_student.php with ‘/’ inside your __autoload() and there you have the exact location of the class file. To me, PEAR like file naming convention is neat, ordered and easy to remember and hence it is best to stick with PEAR-like naming convention so that it will make your life easier when you use __autoload() function. Either way, there are several approaches by which you can tailor your __autoload() to cater
your requirement. I do not claim that using __autoload() was the best approach to include files as shown above, there are several other approaches which does not include the use of __autoload().

A few things to keeping in mind while using __autoload() is that a call to this function is not controlled by the person who writes the code but PHP does it automatically when necessary. So, making this built-in function as a part of your custom class is pure nonsense unless you have a specific reason for it. As always, care should be taken that the developer does not name another custom function as ‘autoload’. Also, autoloading is not available if you are using PHP in the CLI interactive mode. PHP also says that exceptions thrown in the autoload function cannot be caught in a subsequent catch block and hence a fatal error raised by PHP is unavoidable.

The overhead of using __autoload() and, the pros and cons of using __autoload is best left to the gurus for a debate. One thing that any PHP developer should keep in mind is that, whether you want to use __autoload() or do not want to use it is all a tradeoff. You need to decide whether it is ideal for your code or application to have __autoload(). Just because your neighbor is using __autoload() does not mean that you should use it. If you are going to follow a non __autoload() approach of hunting for files and then trying to load
it using a simple ‘include’ then keep in mind that searching a large directory structure for a single file will result in plenty of ‘disk reads’ resulting in the overall slow down of your code/application. If you try to do the opposite of trying to load all files irrespective of whether it is required or not will lead to the wastage of memory. Hence, as I said before, whether to use __autoload() or some other approach is purely a decision to be made by the developer based on what is best for the application.

So, until next time, it is Happy PHPing!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: