PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP Namespaces

PHP OOP Namespaces

Namespaces allow you to have multiple classes with the same name.


Explanation 1

Assume that we have a variable named $title in the global scope of a PHP script. Inside a function, we have another variable with the same name $title, and we assign and change values of it within the function. But, the global variable remains unchanged. This is variable scope. In the same way, classes can be namespaced to give it scope.

Explanation 2

Assume that you are creating an open-source PHP library to send emails and you share that with the developer community. Your library has a class named Email. Then, a developer downloads your library. But, if he already had a class called Email, Name Collision occurs. You will need to rename those classes or use a smarter solution: Namespaces.

Explanation 3 - The Simplest

Two people can be named "Jack". We can separate them by their surname. In the same way, two classes can have the same name. We can separate them by namespacing.

What can namespaces do?

You will learn how to do both of this in this chapter.

Why Namespaces in PHP?

In PHP, you cannot have two classes with the same name. If you need two classes to handle your blog users and app users, you will need to name those: Blog_User and App_User. But, those prefixes are hard to manage.

Namespaces can be used to simplify the process and make it more organized. Namespaces give you more control.

Namespaces and File Structure

The concept of defining and calling namespaces is similar to the normal file structure in our operating systems.

Think namespaces as folders and files as classes.

Namespaces work in the same way.

Global Namespace

When you declare a class (or function or constant) without a namespace it can be called "an item in the global namespace".

How to Define Namespaces?

The namespace keyword followed by the namespace name is used to declare a namespace in PHP.

namespace MyApp;

Namespace name should be a valid PHP identifier. Namespace declaration should be the first thing in a PHP file. (with exception of declare keyword).

Declaring PHP Namespaces
Declaring PHP Namespaces

Any valid PHP code can be in a namespace, but only classes, interfaces, constants, and functions are affected. Even it is possible to have multiple namespaces in the same file, it is not recommended.

Creating and Accessing Namespaced Classes

One special thing to remember throughout this tutorial: PHP is always relative to the current namespace.

If we add the namespace declaration at the top of a PHP file, all the classes, functions and constants will be items of that namespace.

Items of Namespaces
Items of Namespaces

Let's create a file named Math.php and add the following code.


namespace Math;

function add($a, $b) {
	return $a + $b;
const PI = 3.14;

class Geometry {
	static function getCircleArea($radius) {
		return PI * $radius ** 2;

Here comes the interesting part. Let's create another file called usage.php and access the above Math namespace's items (functions, constants, and classes).


// includes the Math.php file
// It's like you had all the code of Math.php written here
include_once 'Math.php';

echo Math\add(2,3); // 5
echo Math\PI; // 3.14
echo Math\Geometry::getCircleArea(10); // 314.15

The \ character is used to go a level down in namespaces.


In an operating system, folders can contain folders. In the same way, namespaces can contain namespaces. They are called sub-namespaces.

namespace Math\Geometry;

// ... your geometry classes

Hereafter, we will only be using classes in namespaces. (No more functions and constants as this is the OOP section in this tutorial)

We will learn all the recommendations for Object-Oriented programming in PHP in a later chapter. But, here we will learn how to structure your namespaces as it is really important to know.

It is recommended to map namespaces with local directories of your computer. See this example:

These are recommendations. Not rules. You can follow them if you like.

Example - Math Namespace

Let's understand more by doing. In this example, we will be creating a namespace named Math and add sub-namespaces, classes into it. Here's the structure.

Here's the final structure of our project.



namespace Math;

class Constants {
	const PI = 3.14159;


namespace Math\Geometry;

class Circle {	
	public $radius;
	public function __construct($radius) {
		$this -> radius = $radius;
	public function getDiameter() {
		return $this -> radius * 2;
	public function getArea() {
		// (pi)(r^2)
		return \Math\Constants::PI * $this -> radius ** 2; 
	public function getCircumference() {
		// 2(pi)(r)
		return 2 * \Math\Constants::PI * $this -> radius;

In Circle.php, we used \Math\Constants::PI to refer the constant in Constants.php file. This is because PHP is always relative to the current namespace. When a namespace starts with a backslash (\), the name will be resolved relative to the global namespace.

  • If we used Constants::PI in Circle.php, it would refer \Math\Geometry\Constants::PI.
  • If we used Math\Constants::PI (without starting backslash) in Circle.php, it it will refer to \Math\Geometry\Math\Constants::PI

Simply, class names are relative to the current namespace. Starting backslash (\) is used to go back to the start. Relative namespaces aren't currently supported in PHP. (../Constants::PI is not supported)

Finally, let's use our classes in index.php (which should be in the root folder of your project)

include_once 'src/Math/Constants.php';
include_once 'src/Math/Geometry/Circle.php';

$circle = new  Math\Geometry\Circle(10);

echo $circle -> getDiameter(); 		// 20
echo $circle -> getArea(); 			// 314.159
echo $circle -> getCircumference(); // 62.8318

Important to note that index.php is in the global namespaces as it is not in any namespace.

Using "use" in Namespaces to Import

The use keyword can be used to import a class to the current scope.

If you add the following code in your index.php, Math\Geometry\Circle class will be imported to the current scope.

use Math\Geometry\Circle;

$circle = new Circle(10); // as Circle class is now in this namespace

You can import both namespaces and classes

use Math\Geometry; // importing an namespace
use Math\Geometry\Circle; // importing a class

You can nickname the imports

There are two places to use nicknames for importing classes.

The as keyword is used for nicknaming.

use Math\Geometry\Circle as Circ;

$circle = new  Circ(10);

Changing the previous example

Let's change our index.php of the previous example adding use statements to import the class.

// ... import statements

use Math\Geometry\Circle;
$circle = new Circle(10);

// ... calling methods

As we did not have an as keyword, the imported class could be referred to using Circle. We can also import the Circle class with a nickname.

use Math\Geometry\Circle as C;
$circle = new C(10);

Wait! It is not a good practice to use names like C. Always use descriptive names.

Here's a task for you: Use use as in the Circle.php and replace the \Math\Constants::PI with a short name.

Organizing Code with Namespaces

One of the main uses of namespaces is for organizing codes. Let's assume that you are creating an open-source library.


In this tutorial, we learned what are namespaces, why do we use them, and how to use them. Namespaces make your project more structured and easy to manage when used correctly. You can use your own way to do that, but follow the recommendations as most of the developers do that.

Did you notice something annoying? If we had a large number of files to include, do we need to include them at the top of each page? Thousands of include statements in a single file? No, we will be discussing a smarter solution for this in the next chapter: Autoloading.

Facebook Twitter