Menu
Coddy logo textTech

Introduction to Namespaces

Part of the Object Oriented Programming section of Coddy's PHP journey. Lesson 10 of 91.

As your PHP projects grow, you'll create many classes. What happens when two classes have the same name?

For example, you might have a User class for your blog and another User class for your shop. Namespaces solve this problem by organizing classes into logical groups.

A namespace is like a folder for your classes. You declare it at the top of your PHP file using the namespace keyword:

<?php
namespace Blog;

class User {
    public $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
}

Now you can have another User class in a different namespace:

<?php
namespace Shop;

class User {
    public $email;
    
    public function __construct($email) {
        $this->email = $email;
    }
}

To use a namespaced class, you reference it with its full path using a backslash:

<?php
$blogUser = new \Blog\User("Alice");
$shopUser = new \Shop\User("alice@example.com");

echo $blogUser->name . "\n";
echo $shopUser->email . "\n";

Output:

Alice
alice@example.com

The backslash \ separates namespace levels, similar to how slashes separate folders in a file path. You can also create nested namespaces like App\Models\Blog for deeper organization.

Key Point: Namespaces prevent naming conflicts and keep your code organized. Always declare the namespace before any other code in your file (except for declare statements).

challenge icon

Challenge

Easy

Let's organize a small application using namespaces to prevent naming conflicts between classes with the same name.

Imagine you're building a system that handles both vehicles and animals. Both domains have an entity called Car — one represents an actual automobile, while the other is a pet cat named "Car" (yes, some people name their cats that way!).

You'll create three files:

  • Vehicle/Car.php — Define a Car class in the Vehicle namespace with a public $brand property. The constructor should accept the brand name and set it. Add a describe() method that returns "Vehicle: [brand]".
  • Animal/Car.php — Define a Car class in the Animal namespace (this is a cat named Car!). It should have a public $age property. The constructor accepts the age and sets it. Add a describe() method that returns "Cat named Car, age: [age]".
  • main.php — Include both class files using require_once. Create one object from each namespace using their full namespace paths, then print the result of calling describe() on each object (each on its own line).

In main.php, create:

  • A Vehicle\Car with brand "Toyota"
  • An Animal\Car with age 3

Remember to use the backslash \ to reference namespaced classes with their full path when creating objects.

Try it yourself

<?php
// Include the class files
require_once 'Vehicle/Car.php';
require_once 'Animal/Car.php';

// TODO: Create a Vehicle\Car object with brand "Toyota"
// Remember to use the full namespace path with backslash

// TODO: Create an Animal\Car object with age 3
// Remember to use the full namespace path with backslash

// TODO: Print the result of describe() for each object (each on its own line)

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming

Practice on your own: Online PHP compiler