Magic Methods – __autoload() method

Learn 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. Every 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.

No comments: