A namespace in PHP organizes classes, interfaces, traits, functions, and constants into logical groups. Namespaces help prevent naming conflicts when an application contains many classes or uses third-party libraries.
Namespaces are especially useful in large projects such as student management systems, library management systems, and web applications.
A namespace provides a way to group related PHP code under a specific name.
Use the namespace keyword to declare a namespace.
A namespace declaration must appear before ordinary code
in a PHP file, except for an initial
declare statement.
<?php
namespace Soopro;
class Student {
public function showMessage(): void {
echo "Welcome to PHP Namespaces.";
}
}
$student = new Student();
$student->showMessage();
?>
Output:
Welcome to PHP Namespaces.
The Student class belongs to the
Soopro namespace. Its fully qualified name
is \Soopro\Student.
Suppose two parts of an application need classes named
Student. Without namespaces, declaring both
classes with the same name in the same global namespace
causes a naming conflict.
Namespaces allow the same short class name to exist in different namespaces.
<?php
namespace School;
class Student {
public function show(): void {
echo "School Student";
}
}
namespace Library;
class Student {
public function show(): void {
echo "Library Student";
}
}
?>
Save the following as a single PHP file to run this example with both namespace declarations:
<?php
namespace School {
class Student {
public function show(): void {
echo "School Student";
}
}
}
namespace Library {
class Student {
public function show(): void {
echo "Library Student";
}
}
}
$schoolStudent = new \School\Student();
$libraryStudent = new \Library\Student();
$schoolStudent->show();
echo "<br>";
$libraryStudent->show();
?>
Output:
School Student
Library Student
The two classes can coexist because their fully qualified names are different.
A fully qualified class name begins with a backslash and specifies the complete namespace path.
<?php
namespace Soopro\Courses;
class PHP {
public function show(): void {
echo "PHP Programming Course";
}
}
?>
If this code is stored in PHP.php, another
PHP file can access the class using its fully qualified
name:
<?php
require_once "PHP.php";
$course = new \Soopro\Courses\PHP();
$course->show();
?>
Output:
PHP Programming Course
The leading backslash means PHP should resolve the name from the global namespace.
The require_once statement loads the file;
the namespace itself does not automatically load it.
The use keyword lets you import a class,
interface, or trait name into the current namespace
or file scope, allowing you to use a shorter name.
File: Student.php
<?php
namespace Soopro\Students;
class Student {
public function show(): void {
echo "Student class loaded.";
}
}
File: index.php
<?php
require_once "Student.php";
use Soopro\Students\Student;
$student = new Student();
$student->show();
?>
Output:
Student class loaded.
The import statement must appear at the appropriate top-level position before ordinary executable code. The required class file must also be loaded or available through an autoloader.
An alias provides a shorter or alternative name for an imported class or namespace.
<?php
namespace Soopro\Library;
class Book {
public function show(): void {
echo "Book information.";
}
}
File: index.php
<?php
require_once "Book.php";
use Soopro\Library\Book as LibraryBook;
$book = new LibraryBook();
$book->show();
?>
Output:
Book information.
The alias LibraryBook is used instead of
writing the complete class name every time.
Aliases are particularly useful when importing classes that share the same short name.
Namespaces can also organize functions and constants, not just classes.
File: helpers.php
<?php
namespace Soopro\Helpers;
function greet(string $name): string {
return "Hello, " . $name;
}
const SITE_NAME = "Soopro Pathshala";
?>
File: index.php
<?php
require_once "helpers.php";
echo \Soopro\Helpers\greet("Rahul");
echo "<br>";
echo \Soopro\Helpers\SITE_NAME;
?>
Output:
Hello, Rahul
Soopro Pathshala
The fully qualified names identify the function and
constant inside the Soopro\Helpers
namespace.
Namespace imports for functions and constants can also
be declared with use function and
use const.
Namespace names can contain multiple segments separated by backslashes. This allows you to organize related classes into subgroups.
<?php
namespace Soopro\Admin;
class Dashboard {
public function show(): void {
echo "Admin Dashboard";
}
}
A student dashboard can belong to a different namespace:
<?php
namespace Soopro\Student;
class Dashboard {
public function show(): void {
echo "Student Dashboard";
}
}
Example usage in another file:
<?php
require_once "AdminDashboard.php";
require_once "StudentDashboard.php";
$admin = new \Soopro\Admin\Dashboard();
$student = new \Soopro\Student\Dashboard();
$admin->show();
echo "<br>";
$student->show();
?>
Output:
Admin Dashboard
Student Dashboard
This structure makes it easier to separate administrative functionality from student-facing functionality.
Code declared without a namespace belongs to the global namespace. Inside a named namespace, a leading backslash can be used to refer explicitly to global classes and functions.
<?php
namespace Soopro\Demo;
class Example {
public function show(): void {
echo "Named namespace example.<br>";
echo \strlen("PHP");
}
}
$obj = new Example();
$obj->show();
?>
Output:
Named namespace example.
3
Here, \strlen() explicitly refers to PHP's
global strlen() function.
PHP has specific fallback rules for unqualified function and constant names inside namespaces, but explicitly qualifying a global name can make the intention clearer.
A student management application can use namespaces to separate student-related classes from course-related classes.
File: Student.php
<?php
namespace Soopro\Students;
class Student {
private string $name;
private int $studentId;
public function __construct(
int $studentId,
string $name
) {
$this->studentId = $studentId;
$this->name = $name;
}
public function showDetails(): void {
echo "Student ID: " . $this->studentId . "<br>";
echo "Student Name: " . $this->name . "<br>";
}
}
File: Course.php
<?php
namespace Soopro\Courses;
class Course {
private string $courseName;
private float $fee;
public function __construct(
string $courseName,
float $fee
) {
$this->courseName = $courseName;
$this->fee = $fee;
}
public function showDetails(): void {
echo "Course: " . $this->courseName . "<br>";
echo "Fee: Rs. " . number_format($this->fee, 2) . "<br>";
}
}
File: index.php
<?php
require_once "Student.php";
require_once "Course.php";
use Soopro\Students\Student;
use Soopro\Courses\Course;
$student = new Student(101, "Rahul Kumar");
$course = new Course("ADCA", 10000);
echo "<h3>Student Details</h3>";
$student->showDetails();
echo "<h3>Course Details</h3>";
$course->showDetails();
?>
Output:
Student Details
Student ID: 101
Student Name: Rahul Kumar
Course Details
Course: ADCA
Fee: Rs. 10,000.00
Namespaces help separate application components and prevent naming conflicts. In larger projects, you can combine them with Composer's autoloading system to load class files automatically.
1. Which keyword declares a namespace in PHP?
2. Which keyword imports a class name into the current namespace or file scope?
3. What is one main benefit of namespaces?