How to Autoload Classes in PHP


In this article, we show how to autoload classes in PHP.

This means that we automatically load class files into a PHP script so that they can be used, even though they aren't in the current PHP script.

It's a lot like the include or require PHP functions, in that it includes another PHP script in the current script, as if that external PHP file was written into the current script. But it's a bit different in that it gets that script and looks specifically for a class that you mention.

With autoloading, you create an __autoload() function somewhere in your script. This function should accept a classname as an argument. Then, whenver your script attempts to create a new object from a class that doesn't exist in your current script, __autoload() is automatically called, passing in the class name. This allows your __autoload() functcion the opportunity to find and include the class file, thereby allowing the PHP script to carry on and create the object you are trying to create with the class name you have used.

So, again, the __autoload() function is called if the PHP script cannot find the class you are referencing when you are creating an object of a class. If not in the current script, it looks for it in the PHP file you specify in the __autoload() function.

The function stores the name of the nonexistent class in a $classname parameter. You then would normally use a require or require_once function to include the script in the function. The function will then go through the script to find the class name you specified.

So let's go through an example now of an autoload function.

__autoload() Function Code

The following below is a very common and used __autoload() function example.

So you can see that this is one PHP file.

In this PHP file, we have an __autoload() function that accepts a $classname parameter which represents the class name of any object we are trying to instantiate. In this example, we try to instantiate the object $elephant1 through the elephant class. So elephant then becomes the $classname parameter passed into the __autoload() function.

Since there is no elephant class in the current script, the __autoload() function is automatically called, so that this elephant class can be found.

Inside of this elephant.php file must be our elephant class.

So if we have an elephant.php file with the elephant class, this file should work.

So now let's look at the elephant.php file shown below.

You can view the contents of this at the following link: elephants.

So now look at this elephant.php file, you can see it has an elephant class, what we are looking for.

Inside of this elephant class, we have a public property of $color equal to the value of brown. This is in the elephant class.

Going back to the PHP file with the __autoload function, we are able to create the $elephant1 object because the elephant class has been found. The $elephant1 object has now been instantiated.

Since the elephant class has the property $color equal to brown, we output that the $elephant1 object is brown.

And this is how the PHP __autoload() function works.

It's always a good practice to name a PHP file and class the same name. And it's a good idea to keep one class per file. So if you are creating a Toy class, you would put in a separate Toy.php file. This makes for good organization. And this makes the __autoload function work. So the __autoload function would not work above if the file and class name were different.

If you need to use more than one class, nothing changes with the __autoload function. For each occurrence of a new class it meets in the code, it will invoke the __autoload() function. So if you have multiple statements that call classes that aren't in your current script, then each time a new class is called, the __autoload() function will be invoked in order to try and find that class. An example of object instantiation from multiple classes can be found at the following link: Multiple Classes.

If the PHP script can't find an __autoload() function or if your __autoload() function fails to find the class in the file specified, the script withts with a Class Not Found error.

So going back to the original PHP script above, if this is run, this yields the following output shown below.

Actual PHP Output

This elephant is brown

Related Resources

How to Write JSON with PHP

HTML Comment Box is loading comments...