Namespaces in PHP

Static Properties in PHP OOP

What are namespaces in PHP?

Namespaces in PHP are also described as qualifiers that provide two main functions to the program.

  1. Using namesapce organize the code by grouping the classes of same nature into a single namespace.
  2. Namespace allows us to use same name for more than one class because they are wraped in the namespace.

General Example of Namespaces

For instance, we have a group of classes that perform the basic mathematical operations DMAS (Division, Multiplication, Addition, and Subtraction). We also have another group of classes that prepare the result of the students of a class.

Namespaces allow us to organize these two sets of classes into two namespaces. One namespace will contain the maths-related classes and the other will contain result calculation classes.

How to declare Namespace it PHP?

  • Namespace is declared on the top of the PHP script so that it can be available accross the file.
  • namespace keyword is reserved for declaring the namespaces.

Syntax

// Declare a namespace maths:
namespace Maths;

Note: It is mandatory to declare the namespace at the top of the file/ The following code would be wrong.

<?php
echo "Hello PHP!";
namespace Maths;
...
?>

Now, look at the other side of this topic, that is the classes that are part of the namespace. We take the Maths class as an example class the declare it in the namespace.

Create a Division class in the Math namespace

<?php
namespace Maths;
class Division {
  public function divide($numenator, $denominator) {
         $result = $numenator/$denominator;
         echo $result;
  }
}
$div= new Division();
?>

<!DOCTYPE html>
<html>
<body>

<?php
$div->divide(6, 3);
?>
</body>
</html>
  • In the above example, we create a Divison class in the Maths namespace. Focus on the use namespace keyword use on the top of Division class. It defines that this Divison class is a part of Maths namespace.
  • Now, we can access the division method anywhere using the Maths namespace in our code.

How to declare Nested Namespaces?

Let suppose, we want to declare the Maths namespace inside the code namespace.

namespace Code\Maths;

How to use Namespaces in PHP?

  • The class that belongs to a namespace does not needs object. We can access the class in the code which is using the namespace of the class which we want to access without creating its instance.
  • To access a class outside the namesapce, we can do it by attaching the namespace to it.

Example

$div= new Maths\division()
$mul= new Maths\multiplication();

When we have many classes using the same namespace, it’s simpler to use the namespace keyword instead of accessing them as in the above example.

namespace Maths;
$div = new division();
$mul = new multiplication();

In the above example, we access the division and multiplication classes without using Maths\division or Maths\multiplication qualifier.

Using Alias with Namespace

Giving some alias (nickname) to a namespace is easier to write within the code. This is helpful when the namespace is too long or tedious to write again and again.

Example

use Maths as M;
$div = new M\division();

Overview of namespaces in PHP in the official PHP documentation.

Static Properties in PHP OOP

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top