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

How to use Git Alias to increase your productivity (video)
Nov 27, 2022 programming

How to use Git Alias to increase your productivity (video)

Today we are going to talk about Git Alias and how to increase your productivity by adding shorter aliases for commands that we use everyday. ...

8 Min Read Read More
PHP CSV Files
Mar 21, 2023 programming

PHP CSV Files

Today we are going to talk about working with a csv files in PHP. Comma Separated Values (csv) is a common way to store the data in a file and it's important to know how to work with them. ...

5 Min Read Read More
PHP Password Hashing
Mar 22, 2023 programming

PHP Password Hashing

Today we are going to talk about hashing passwords in PHP. Storing and managing passwords is very important. And in PHP it’s very easy. ...

4 Min Read Read More
React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications
Jul 18, 2025 programming

React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications

React JS has firmly established itself as one of the most widely adopted JavaScript libraries for building dynamic user interfaces. Whether you're crafting a small personal website or architecting a large-scale enterprise dashboard, React's component-based architecture and flexible ecosystem allow developers to create highly performant, scalable, and maintainable applications. ...

29 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 ...