Autoloading Classes

 While writing object-oriented programming we have to include class files to call the method of the classes.

Sometimes we have to include a long list of needed includes (one for each class) at the beginning of each script.

Now, in PHP5 there is no longer necessary to includes classes. In PHP5 the spl_autoload_register function registers any number of autoloaders , enabling for classes and interfaces to be automatically loaded if they are not defined, PHP is given a last chance to load the class before throwing error.

 e.g :

<?php

 spl_autoload_register(function ($classname) {

    include  $classname . '.php';

});

 $object  = new Class1();

$object2 = new Class2();

 ?>

In the above example we do not need to include class1.php and class2.php .  spl_autoload_register() function will automatically load class1.php and class2.php .

0   0
Aditya Singh

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site