Free cookie consent management tool by TermsFeed Generator PHP Namespace | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Namespace

PHP Namespace

Last Updated on Mar 23, 2023

Introduction

Do you remember when we talked about Classes and inheritance?

There is an important issue: you cannot have 2 classes with the same name. You’ll get an error.

Namespace is the solution to this problem

What is Namespace?

Namespace is like putting the classes in a group. As long as two class are in a different group they can have the same name. It helps you solve that problem in a very organized manner.

Let ‘s see an example:

Here I have 3 files

  1. Index.php
  2. FirstClass.php
  3. SecondClass.php

In Firsclass.php I have a class called order

<?php
class Order
{
    public function handle()
    {
        echo "done";
    }
}

In the SecondClass.php I also have a class called order

<?php
class Order
{
    public function handle()
    {
        echo "done";
    }
}

Now I include both of those files in my index file

<?php
require './FirstClass.php';
require './SecondClass.php';

Problem

I get an error saying:

// Cannot declare class Order, because the name is already in use

Solution

There are 2 solutions for this:

  1. change the class name
  2. namespace

1. change the class name

This could be a good solution but you can imagine in a large project with many classes naming the classes can be a problem.

class BookOrder
{
    public function handle()
    {
        echo "done";
    }
}

class MovieOrder
{
    public function handle()
    {
        echo "done";
    }
}

then we can use those classes in our index.php file like this:

<?php
require './FirstClass.php';
require './SecondClass.php';

$order = new MovieOrder();
$order->handle();
// done

2. namespace

https://youtu.be/SqLLTS65QCE

Namespace is like putting the classes in a group. As long as two class are in a different group they can have the same name. It helps you solve that problem in a very organized manner.

<?php
namespace Movie;

class Order
{
    public function handle()
    {
        echo "done";
    }
}

and for books

<?php
namespace Book;

class Order
{
    public function handle()
    {
        echo "done";
    }
}

This is a better and cleaner solution. Now I can include both of them without any error. When I want to use those classes I should specify which group. So it would be something like this

<?php
require './FirstClass.php';
require './SecondClass.php';

$order = new Movie\Order();
$order->handle();
// done

With Movie\Order() I’m telling php that I want to use the class "Order" that is in "Movie" namespace.

One important note: Namespace should be declared before anything else. This should be the first thing in your class file. Nothing before it should be echoed or printed. Otherwise you’ll get an error

Conclusion

Now you know about namespace in PHP.

I recommend you to open multiple PHP files and create classes with the same name but put them in different namespaces and try to use them in your index.php file.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • what is namespace
  • what problem is it going to solve

Category: programming

Tags: #php #oop

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

the ultimate guide to Git: learn everything you need in less than 1 hour
Nov 28, 2022 programming

the ultimate guide to Git: learn everything you need in less than 1 hour

Here is everything you need to know about git in a series of short question and answers. ...

11 Min Read Read More
PHP Operators
Mar 21, 2023 programming

PHP Operators

Today we are going to learn about Operators in PHP including arithmetic, comparison and logic operators. You will be using all of these operators during your career a lot. ...

5 Min Read Read More
PHP array change key case
Feb 14, 2023 programming

PHP array change key case

Today we are going to talk about changing the case of the keys of an array in PHP. Sometimes you might change the case of the keys in your array. It's very easy to do that in PHP. ...

4 Min Read Read More
Docker Beginner to Advanced 2025: The Complete Guide for Developers
Jul 18, 2025 programming

Docker Beginner to Advanced 2025: The Complete Guide for Developers

Containers have transformed the way we develop, deploy, and scale applications. Docker leads the charge, offering a developer-friendly platform to create consistent, portable environments from development to production. ...

27 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...