Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔴 Advanced  ·  Lesson 21

Namespaces in PHP (with Examples)

In this lesson: Avoid class name clashes with PHP namespaces — declare, use, alias and folder structure explained with examples.

The problem namespaces solve

In a big project, you might have your own User class, and a library you installed might also have a User class. PHP cannot have two classes with the exact same name — it causes a "cannot redeclare" error. Namespaces fix this by giving code a group name, like a surname, so the two User classes can coexist.

💡 Think of a namespace as a folder-like label for your code: App\Models\User is a different "User" from Vendor\Auth\User.

Defining a namespace

Declare it as the first statement in the file.

<?php
namespace App\Models;

class User {
    public function greet() {
        return "Hello from App\\Models\\User";
    }
}
?>

Now this User belongs to App\Models. Another User in a different namespace will not clash with it.

Using it with use

Import the class with use, then call it by its short name.

<?php
require "User.php";
use App\Models\User;

$u = new User();
echo $u->greet();
?>
Hello from App\Models\User

Without use, you would have to write the full path every time: new \App\Models\User(). The use line keeps your code short and clean.

Aliasing with as

If two classes share a name, rename one locally with as.

<?php
use App\Models\User as AppUser;
use Vendor\Auth\User as AuthUser;

$a = new AppUser();
$b = new AuthUser();
?>

Now both "User" classes are usable in the same file under different local names — no clash, no confusion.

Sub-namespaces

Namespaces can be nested with backslashes to mirror your folder structure.

<?php
namespace App\Controllers\Admin;
// full name: App\Controllers\Admin\DashboardController
class DashboardController { }
?>

By convention App\Controllers\Admin matches the folder app/Controllers/Admin/. This tidy matching is what lets autoloaders find classes automatically.

Common mistakes

  • namespace not first. The namespace line must be the first statement after <?php.
  • Forgetting use. Then you must write the full path or PHP cannot find the class.
  • Mixing / and \. Namespaces use backslashes \, not forward slashes.
  • Name clash after import. Two same-named imports need as to rename one.

Summary

  • Namespaces group code so classes with the same name do not clash.
  • Declare with namespace App\Models; as the first statement.
  • use imports a class so you can use its short name.
  • as renames an import to avoid clashes.
  • Namespaces usually mirror folders and power autoloading.

Next, load namespaced classes automatically with PHP autoloading, or structure code with the MVC pattern.

Frequently Asked Questions

What problem do namespaces solve in PHP?
As projects grow, two classes might have the same name — for example your User and a library's User. Namespaces group code so these do not clash, by giving each a kind of "surname" (its namespace).
How do I define a namespace?
Put namespace App\Models; as the very first line of the file (after <?php). Everything in that file then belongs to the App\Models namespace.
What does the use keyword do?
use imports a class from a namespace so you can call it by its short name instead of the full path. use App\Models\User; lets you write new User() instead of the long form.
What is aliasing with as?
If two imported classes share a name, or a name is long, you can rename it locally: use App\Models\User as AppUser;. Then you use AppUser in that file.
Are namespaces the same as folders?
Not exactly, but they usually match by convention. A class App\Models\User is typically in app/Models/User.php. This matching is what makes autoloading work smoothly.
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicAutoloading Classes Your progress: 21 / 50 (42%) · Lesson 22 / 50 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free