Broad Network

The Need and Meaning of a Namespace in PHP

PHP Namespace - Part 1

Foreword: In this part of the series, I talk about the need and meaning of a namespace in PHP.

By: Chrysanthus Date Published: 30 Jan 2014


This is part 1 of my tutorial series, PHP Namespace. In this part of the series, I talk about the need and meaning of a namespace in PHP. My volume, PHP Course is divided into two portions. The first portion is the professional course; the second portion is the advanced course to make you a master (in PHP). The outputs of the code samples in this series are to the browser.

Back to the story at hand: The Need and Meaning of a Namespace in PHP. PHP namespace prevents entity name collisions as you use the entities. Get the details as you read along.

Note: in this article, if you cannot see any text or piece of code or if you think something (e.g. an image) is missing or link does not operate, just contact me at .

This series is part of my PHP Course volume. This series is of the advanced course. So you should have completed all the series before this one, in the volume. This series is a continuation in the volume (but toward the end). To know what you should have already covered, just click the link, “PHP Course” below.

A problem
Read and try the following code:

    function myFn()
            echo "seen";



The compilation and execution (interpretation) of the above code went well.

Note, a program can never be executed unless it is compiled successfully.
Now, read and try the following code where another function is defined. The two functions have the same name, myFn.

    function myFn()
            echo "seen";

    function myFn()
            echo "not seen";



The program did not compile; the program did not run. There are two functions with the same name in the code. That is ambiguous! The reason for this error is because you cannot have more than one function with the same name in the same scope (see details later). The scope in question here is the global scope.

When talking about namespaces, we are talking about entities, in a way that you should use them.

To solve the above name collision, the developers of PHP came up with the idea of namespaces. It is simple: the whole file is declared as one namespace or you have the entities of your code in a block (pair of curly braces). That block is a namespace. The block is given a name by you. The name of the namespace is not preceded by $.

Meaning of PHP Namespace

PHP Names
The following PHP entities each has a name: object, class, array, variable, function, interface and constant. PHP namespace is for classes, interfaces, functions and constants. As of now, it is not for variables, arrays and objects.

PHP has the global scope and the function scope.

The global scope is for any name declared outside function blocks. The function scope is more or less the function block.

The namespace scope is defined in a similar way to the function scope.

Same name of different Entity-Types in the same Scope
Can you have more than one entity of different types, but with the same name, in one scope? No.

Same Name in Different Scopes
You can have different entities of the same name in different scopes; the entities can be of the same type or of different types.

Namespace as a Consolidation of Scopes
The namespace was developed to solve the problem of name collision within a scope and among scopes. The problem solved is to give the possibilities to use the same name in different scopes and within a scope; this may not make sense to you now, but as you read on, it will be illustrated and clarified. A namespace is a scope, which comes to consolidate different scopes so as to prevent name conflicts (collisions).

A namespace can be defined to be the whole file or in a block (within pair of curly braces) as indicated by the following syntax:

    namespace namespaceName
            //Entity definitions

Namespace Members
As of now, the possible useful definitions you can have in a PHP namespace are of classes, interfaces, functions or constants.

This has been a rather theoretical tutorial. You will see more concrete aspects in the following parts of the series.


Related Links

Basics of PHP
PHP Directory Function Basics
Understanding PHP Reference
PHP Function Arguments
Understanding Variable Scope in PHP
Object Oriented Programming in PHP
PHP Data Types Simplified
Exception Handling in PHP
PHP Regular Expressions
Sending Email with PHP
PHP Strings
Date and Time in PHP
PHP String Functions for Website Design
Advanced Course
PHP Variable Scope
Advanced Features of PHP Function
Array in PHP
PHP Two Dimensional Arrays
Understanding Object Oriented Programming in PHP
Advanced PHP Regular Expressions
Some features of PHP Entities
PHP Namespace
PHP Web Application

More Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course



Become the Writer's Fan
Send the Writer a Message