PHP language article

Home >> PHP language >>

  How does the autoload works in PHP

This article will explain what is the benefit of autoloader in PHP and how it works. We know that we can use some functions to include code from other files in a file we are working on. These are the functions:

 include
 include_once
 require
 require_once

Now, imagine we have this file tree in our application:

 classes/
     user.php
 index.php

Directory classes/ has one file and the file index.php is the application's entry point. If we want to use the class user.php in index.php, we should do something like this:

 include "classes/user.php";
 $user = new User();

No problems so far, it's just one class after all. But if we need four classes to be loaded:

 include "classes/user.php";
 include "classes/customer.php";
 include "classes/order.php";
 include "classes/invoice.php";

 $user = new User();

Ok, now it's a boring job to do. Actually, this is a job for the autoloader. This feature does exactly what the name says, it automatically loads a class when it is needed.
    
Setting up an autoloader is pretty simple. Just create a function that receives a class name do load and do the inclusion of its file. We have two functions to do it, those functions have their own benefits and imitation.

1. __autoload()
2. spl_autoload_register()

1. __autoload()
__autoload is generally considered obsolete. It only allows for a single autoloader. Generally, you should only use __autoload if you're using a version of PHP without support for spl_autload_register, read more about PHP __autoload

2. spl_autoload_register()
spl_autoload_register allows several autoloaders to be registered which will be run through in turn until a match is found, read more about PHP spl_autoload_register

Comments

PHP

wow really nice and useful blog and explanation are very clear so easy to understand.. thanks a lot for sharing

php training institute | best php training institute | best php training institute in chennai | php training institute in chennai

  1 year ago      Credo Systemz      
php training in chennai

Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area. php training in chennai

  6 months ago      inigojoseph      
aws training in marathahalli

Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area. aws training in marathahalli

  5 months ago      inigojoseph      
Final Year Project Center in Velachery

Great Article shared and the information was very useful
Final Year Project Center in Chennai | Final Year Project Center in Velachery

  4 months ago      Sara      
Java

Your post is awesome..Your blog is explain each and every concept is good...Java Project Center in Chennai | Java Project Center in Velachery

  4 months ago      sanjay      
Educations

The Information which you provided is very much useful for me. Thank You for Sharing Valuable Information...
Photoshop Training Institute in Chennai | Photoshop Training Institute in Velachery

  4 months ago      nandhu      
multimedia

Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..
CorelDraw Training Institute in Chennai | Photoshop Training Institute in Chennai | CorelDraw Training Institute in Velachery

  4 months ago      sarath k      
Best aws training in chennai

Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.

AWS Training in Chennai

  3 months ago      mari      
Best PHP Training in Chennai

The very useful content you have shared. Really great. Thank you.

PHP Training in Chennai
PHP Course in Chennai

  1 month ago      Steve Mark      
network+

Awesome blog, Really your blog was so informative. Thanks for giving such a useful information, kindly keep updating .
Comptia N+ Certification Center in Chennai | No.1 Network+ Course in Medavakkam

  1 month ago      sheeran      

Write your comment now