Understanding Why Multiple use Statements Are Needed in PHP AutoloadingWhy Composer Autoloading Isn’t Magic (But Still Saves Your Butt)

When using Composer’s PSR-4 autoloading, it’s common to place multiple classes inside the same src/ directory under a single namespace.

📌 Meaning of the autoload section

"autoload": {
    "psr-4": {
      "DilipParmar\\": "src/"
    }
}

🔹 1. Autoload

This tells Composer how to automatically load your PHP classes so you don’t have to manually require or include files.


🔹 2. PSR-4 Autoloading

psr-4 is a standard for mapping namespaces to directory paths.

It says:

“When a class uses this namespace prefix, load it from this folder.”


🔹 3. What "DilipParmar\\" : "src/" means

Namespace Prefix → Directory

  • Namespace prefix: DilipParmar\
  • Corresponding folder: src/

Composer will look inside the src/ directory whenever it sees a class starting with the DilipParmar namespace.

Example

If you have a class:

<?php
namespace DilipParmar\Controllers;

class UserController {
}

Composer expects the file to be located at:

src/Controllers/UserController.php

The namespace structure must match the folder structure.

When using Composer’s PSR-4 autoloading, it’s common to place multiple classes inside the same src/ directory under a single namespace. For example:

src/
  DB.php
  Load.php
  Dummy.php

with each file containing:

namespace DilipParmar;

At first, it seems logical to expect that importing the namespace once would allow using all classes inside it. However, this often leads to confusion when PHP automatically adds separate use statements for each class.

This happens because of how PHP namespaces work.


Composer Autoload vs PHP Namespace Rules

Composer’s autoloading does exactly what it should:
it loads classes automatically when they are needed.

But importing classes is a PHP language rule, not a Composer feature.

In PHP:

  • You cannot import an entire namespace at once.
  • You must import each class individually.

So instead of:

use DilipParmar;

you must write:

use DilipParmar\DB;
use DilipParmar/Load;
use DilipParmar/Dummy;

This is normal and expected behavior.


Cleaner Option: Grouped Imports

To make this shorter, PHP allows grouping:

use DilipParmar\{DB, Load, Dummy};

This still imports each class individually, but in a cleaner way.


Alternative: Use Fully Qualified Class Names

If you prefer not to write any use statements, you can create objects with full namespaces:

$db = new \DilipParmar\DB();
$load = new \DilipParmar\Load();
$dummy = new \DilipParmar\Dummy();

This also works correctly with Composer autoloading.


Conclusion

Your autoload configuration is correct.
The requirement for multiple use statements comes from how PHP handles namespaces, not from Composer.

Composer loads your classes automatically, but PHP still needs to know which specific class names you want to use in your file. This is why each class requires its own import or must be called with its full namespace.

To put it very simply:

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

tells Composer:

“Whenever you see a class that starts with the namespace App\, look for its file inside the app/ directory, following the folder structure that matches the namespace.”

So, for example:

namespace App\Controllers;

class HomeController { }

Composer expects the file to be at:

app/Controllers/HomeController.php

You can then use it in your code without manually requiring the file:

use App\Controllers\HomeController;

$controller = new HomeController();

It’s the PSR-4 autoloading standard in action.