PHP Namespace: Syntax, How to Declare, Use with Examples

PHP Namespace is the way to group together multiple classes and solve one of the most common issues, using the same name with another class.

What is PHP Namespace

Using PHP Namespace we can group multiple classes together to perform a task, Namespace also allows us to use the same name in more than one class.

For example, if we are creating classes for two different kinds of tasks, i.e.-

  • One set of classes for HTML Tables, rows, and cells.
  • Another set of classes for actual physical furniture is Tables and chairs.

For ease of use, we have to name it Table but if we do that without using Namespace, PHP will throw an error.

Namespace would group classes in different groups like one for HTML Tables and one for Furniture tables.

Since these are in two separate groups we can use the same Classname without getting an error.

How to Declare a Namespace

To declare a Namespace in PHP, we use its keyword namespace and then the name of our Namespace.

Here is the syntax for it-

namespace NameOfNameSpace;

Also, one thing that we must have to remember is that namespace should be the first thing after PHP opening tags.

If we place it anywhere else it will not work and will be invalid.

Also once we declare the namespace all the classes from there will be inside that namespace.

Once we created a namespace and some classes for it we have to next create an instance for those classes.

Outside of Namespace, here is how we can create objects for those classes-

$object = new NameOfNameSpace/NameOfClass();

Furthermore to make our code more organized we also have the option to create nested Namespace.

namespace Furniture/OfficeFurniture;

In the code above we have declared the nested namespace; which means a namespace inside a namespace.

How to use a PHP Namespace?

We learned that once we declare the namespace in the PHP file, every class from there on that file would be in that namespace.

and we have to declare the namespace at the start.

Here is a simple example of Namespace, and its classes-

// Declaring a Namespace
namespace Html;

// Creating classes for Namespace

class Table {
  public $title = "";
  public $numRows = 0;
  public function message() {
    echo "<p>Table '{$this->title}' has {$this->numRows} rows.</p>";

// Creating object for classes inside Namespace

$table = new Table();
$table->title = "My table";
$table->numRows = 5;

We can create an object of the namespace class in that namespace the same as we create any object.

But for creating objects outside of that namespace we have to refer to namespace as well as class name as well.

 $table = new Html\Table();

Important facts about Namespace

  • The namespace should be the first thing after PHP opening tags.
  • Using Namespace we can group together multiple classes together to perform a task.
  • In PHP we can’t use a Class name in another class, Namespace solves this problem.
  • We can create a nested Namespace to make our code more organized.
  • To create an object of the class outside the namespace we have to refer to Namespace as well.

About Rahul Bodana

Rahul Bodana is passionate about sharing his knowledge with others and providing useful tutorials and how-to guides. In addition to programming, he also shares information on a variety of topics, including investment, trading, gaming, and writing.