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

Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 Min Read Read More
PHP break and continue
Mar 21, 2023 programming

PHP break and continue

Today we are going to learn about break and continue in PHP. These two words are going to help you a lot when dealing working with loops. you can avoid a lot of extra iterations and save time. ...

5 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
PHP Array Flip
Feb 14, 2023 programming

PHP Array Flip

Today we are going to talk about array flip function in PHP. Array flip is very simple yet useful array function that helps you flip and array. ...

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