Understanding Autoload and Namespace in PHP

Understanding Autoload and Namespace in PHP

Posted on:September 24, 2019 at 10:00 AM

If you come across from procedural PHP programming, autoload and namespace kind of confusing naming for you. To make it easier, today, I will take a deep dive into understanding autoload and namespace in PHP.

Table of Contents

Open Table of Contents

What is Autoloading?

Autoloading is a mechanism where you can load all the required files in your project automatically.

For example, you have a few files-

  • Models\Users.php
  • Models\Tasks.php
  • Models\Posts.php

Now, if I want to use those files in my index.php file, we just use include() or require() function to load like this-

index.php

<?php

include('Models\Users.php');
include('Models\Tasks.php');
include('Models\Posts.php');

...

Now just imagine that you have 20 dependency files to include in index.php file, then you need to use include() so many times. So, isn’t that better to include a single file that is totally responsible to load all the files in our app?

Literally, to solve this issue, the autoloading concept comes in the PHP world.

Namespace

The namespace is a concept to define the exact file location for handling the big project where many files are involved. It allows and makes your life easier to manage and maintain files in different folders.

Practice

Let’s define an empty project called autoload and the structure of your application is like so-

autoload - app - Models - User.php - Controllers - UserController.php - index.php

Now, let’s create a composer.json file in the root directory to load all the required files for this project. The content of the composer.json will be like this-

composer.json

{
	"autoload": {
		"psr-4": {
			"app\\": "app"
		}
	}
}

You have to make sure that, you have installed composer in your computer. If it is not there, then just install from here.

Now, open your terminal and heading to your project folder, then run the following command.

composer dumpautoload

Once you have run the command, you have noticed that there is a new folder in the root directory of your project called vendor.

Now, in your index.php file, you just need to include() the autoload.php file.

index.php


<?php

require 'vendor/autoload.php';

...

Finally, you have to use namespace in your files. For example, in the Models\Users.php file, you have to add namespace on top of your file. So, in the index file, you can easily use of it. An example of this will be like so-

Models\Users.php


<?php
namespace app\Models;

class User
{
	public function all()
	{
		return [
			[
				'name' => 'user1',
				'email' => '[email protected]',
			],
			[
				'name' => 'user2',
				'email' => '[email protected]',
			]
		];
	}
}

Now, you are ready to use your files. In the index.php file, you can call user.php file via autoloading. Let’s see-

index.php


<?php

require 'vendor/autoload.php';

use app\Models\User;

$users = new User;
var_dump($users);

It should return the instance of Models\User class.

Like so, you need to use namespace in every file, so that y0u can call files easily in any place in your application through autoloading.

You can find more in-depth code in this repo

Hope this intro will be helpful for you.

Thank you.