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

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 Simple Web Crawler
Mar 22, 2023 programming

PHP Simple Web Crawler

Today we are going to build a simple web crawler in PHP and parse the box office movies from IMDB. We can do it with PHP easily. We don’t even need any extra package. ...

14 Min Read Read More
How to Combine Excel Files Using PHP
May 06, 2024 programming

How to Combine Excel Files Using PHP

Sometimes you might have many excel files that you need to combine, whether it's reports, user data or anything else, merging them into one file with PHP can be easily done. ...

11 Min Read Read More
Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers
Sep 10, 2024 programming

Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers

While PHP and Python share several core programming concepts, transitioning between the two requires understanding the key differences in syntax, paradigms, and best practices. ...

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