Home > PHP, PHP Tutorials, PHP5 Magic Methods, PHP5 OOPS Tutorials > PHP5 Tutorial – Magic Methods – __autoload() method

PHP5 Tutorial – Magic Methods – __autoload() method

November 3rd, 2007 admin Leave a comment Go to comments

This tutorial will teach you how and when to use the magic method __autoload().

The magic method __autoload() function is a convenience that allows you to use classes without having to explicitly write code to include them.

The magic method __autoload() is not included in your class definition as this is to be called once in a script. The best place to put the autoload() file is in your configuration file which is loaded in all your other scripts.




Many people debate that the magic method __autoload() causes a performance overhead. Well, that is not the case. There is no performance penalty to pay. In fact, there may be performance improvements if not all classes are used all the time. This is explained below.

Using the magic method __autoload has the beneficial side effect of requiring strict naming conventions for files that hold class definitions.

Look at the example below:

include “customer.php”;
include “orders.php”;
 
$c = new Customer();

In the example displayed above, an instance of class Customer is created. Therefore, we only need the customers.php file. The file orders.php is not needed. This means that we should only have included the customer.php file. But, what if during execution on the basis of a condition, an instance of class Orders would have to be created. Therefore you need to include both the files i.e. customer.php and orders.php

But this causes performance issues. Each time the above script is executed, orders.php is included. To avoid this performance hit, we would have to do additional programming to ensure that the file orders.php is loaded only when needed.

This is the reason why magic method __autoload() should be used. Look at the example below:

function __autoload($class) {
   require $class . '.php'; //is substituted as require Customer.php (with capital 'C')
}
 
$c = new Customer();

In the above program, we don’t explicitly include customer.php and orders.php file. When an instance of the customer class is to be created, the PHP engine checks to see if the file Customer.php is loaded. It does not raise an warning on finding that Customer.php has not been loaded, it in turn calls the magic method __autoload(). The __autoload() magic method accepts a parameter which is the name of the class that needs to be loaded.




Therefore, on the line when an instance of the customer class is created i.e. object $c, magic method __autoload() is called with the parameter $class containing value ‘Customer’. Within the __autoload() method we call the ‘require’ method. The require method tries to load $class.’php’ file i.e. Customer.php. Therefore, as stated earlier, the __autoload() method has its beneficial side effect of requiring strict file naming convention.

The __autoload() method is called only once for each new class that needs to be loaded. Subsequent instantiation of the Customer class object will not call the __autoload() method again. Therefore, this offers performance improvements in your scripts because, unless the class is needed – files are not loaded. Therefore, the PHP engine does not have to parse and compile an unnecessary file.

Please feel free to leave behind any comments or questions that you might have.

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Chris
    February 28th, 2008 at 18:28 | #1

    Thanks for this – a very useful function! There is a slight error in the coding however, the require line results in an error and should read:
    require $class . ‘.php’;

  2. chinuku
    December 19th, 2008 at 18:17 | #2

    Hai
    Yours Oops concepts In PHP helps me a LOT
    IF POSSIBLE can u give sme exapmles also

    Thanku So much

    Sree

  3. Santosh
    January 27th, 2010 at 13:44 | #3

    Hai
    Yours Oops concepts In PHP helps me a LOT. i am very thankful to you.

  4. Pawan
    February 12th, 2010 at 08:25 | #4

    Hai
    Yours PHP5 Oops concepts is very very good.
    Helps me a LOT.
    I am very thankful to you.

  1. November 3rd, 2007 at 16:44 | #1
  2. November 8th, 2007 at 22:53 | #2
Enter this code to leave comment (Sorry, but bots get me crazy :) )