PHP

Autoloading In PHP

Autoload is a method in PHP that is used to load automatically all those class files for which you have to use include or required repeatedly in the main file and it’s called autoloading.

You must be thinking you can do the same thing by simply using include or required commands but, Suppose you are working on a complex project where you declared multiple classes so your code Would be a little messy or difficult to understand If you use required or include for each class. 

Autoload is the perfect method for these types of tasks, you can develop complex projects using PHP Autoload Method. 

It is also called the magic method because it starts with double underscores “__”.

Syntax

Autoload(string $class):void

Parameters

You can pass a variable as a parameter in which the class file will be sent.

Example 1

<?php

Function __autoload($class)

{

require ("classes/". $class. ".php");

}


$first = new first();

echo "<br>";

$second = new second();
 

 ?>

In the above example, we used autoload method and passed a variable named ($class) as a parameter, then we set the file path, and then we created the objects. 

Output

welcome First
welcome Second

Note

  • in the $class variable the class file name will be sent and it will be called dynamically means it will be that class that we want to call.
  • “classes” is the folder in which class files will be saved and .php is the extension with them.

So this was PHP autoload method, if we talk about today’s modern era where advanced projects are high in demand so autoload method is not enough for them.

Autoload method is not that much feasible as we want for our advanced and complex projects so PHP introduced the updated version of autoloading method which is “spl_autload_register”.

Spl_autoload_register

The PHP spl_autoload_register method can load multiple functions automatically for each class. You can create your spl_autoload_function, it has replaced the autoload method.

Example 2

<?php

Spl_autoload_register(function($class)

{
      Include 'classes/'. $class. '.php';

});

$First = new First();

echo('<br>');

$Second = new Second();

?>

Output

welcome First

welcome Second

There is another way to perform the above example.

First of all, create separate files for classes and then create two more separate classes, one for the autoloader and the other for the main file in which you will include the autoloader file.

Example 3

Firts.php

<?php

/**
 * 
 */

class First
{
               
               function __construct()

               {
                               echo "welcome First";
               }
}

?>

Second.php

<?php

/**
 * 
 */

class Second

{
               
               function __construct()
               {
                               echo "welcome Second";
               }
}

?>

Autoloader.php

<?php

spl_autoload_register(function($class) {

               $doc = $class . '.php';

               if (file_exists($doc)) {

                               include $doc;
               }
});

?>

Use.php

<?php

include 'autoloader.php';

$first = new First;

echo "<br>";

$second = new Second;

?>

First, we created classes in the separate files then, we created another file in which we declared the spl_autoload_register method.

This method will register the autoloader and find the classes, in the last we created one more file, the main file use.php in which we included our autoloader file.

Output

welcome First

welcome Second

Conclusion

In this tutorial, we discussed the autoload and spl_autoload_register methods, we saw their definitions, and then we discussed examples.

Suggested Article:

Leave a Comment