PHP Namespace

PHP Namespace

Last Updated on Mar 24, 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

Courses