It provides a way of wrapping items from the global space and allows use of plain item name without causing name collision in a program.
It's supported in PHP 5.3.0, PHP 7.
But there are some limits in analogy between PHP namespace and Unix based filesystem:
| Filesystem | PHP Namespace
--------------------------|-----------------------|-------------------------
Cas sensitive | No | Yes
--------------------------|-----------------------|-------------------------
Name with plain number | Yes | No
--------------------------|-----------------------|-------------------------
Path level | Yes | Yes
--------------------------|-----------------------|-------------------------
Plain metacharacters name | Yes | No
The principle extends to namespace in programming word.
Namespacing does for functions and classes what scope does for variables. It allows you to use the same function or class name in different parts of the same program without causing a name collision.
In simple terms, think of a namespace as a person's surname. If there are two people named "John" you can use their surnames to tell them apart.
The Scenario
Suppose you write an application that uses a function named output(). Your output() function takes all of the HTML code on your page and sends it to the user.
Later on your application gets bigger and you want to add new features. You add a library that allows you to generate RSS feeds. This library also uses a function named output() to output the final feed.
When you call output(), how does PHP know whether to use your output() function or the RSS library's output() function? It doesn't. Unless you're using namespaces.
Example
How do we solve having two output() functions? Simple. We stick each output() function in its own namespace.
That would look something like this:
namespace MyProject;
function output() {
# Output HTML page
echo 'HTML!';
}
namespace RSSLibrary;
function output(){
# Output RSS feed
echo 'RSS!';
}
Later when we want to use the different functions, we'd use:
\MyProject\output();
\RSSLibrary\output();
Or we can declare that we're in one of the namespaces and then we can just call that namespace's output():
namespace MyProject;
output(); # Output HTML page
\RSSLibrary\output();
No Namespaces?
If we didn't have namespaces we'd have to (potentially) change a lot of code any time we added a library, or come up with tedious prefixes to make our function names unique. With namespaces, we can avoid the headache of naming collisions when mixing third-party code with our own projects.
Namespace is like packaging many things into a single pack. Imagine a namespace as a drawer in which you can put all kinds of things: a pencil, a ruler, a piece of paper and so forth. To avoid using each other's items, you decide to label the drawers so it's clear what belongs to whom.
Much like directories and files, namespace in PHP serves to group classes, functions, interfaces and constants.
Example:
It provides a way of wrapping items from the global space and allows use of plain item name without causing name collision in a program. It's supported in PHP 5.3.0, PHP 7.
But there are some limits in analogy between PHP namespace and Unix based filesystem:
The principle extends to namespace in programming word.
Namespacing does for functions and classes what scope does for variables. It allows you to use the same function or class name in different parts of the same program without causing a name collision.
In simple terms, think of a namespace as a person's surname. If there are two people named "John" you can use their surnames to tell them apart.
The Scenario
Suppose you write an application that uses a function named
output()
. Youroutput()
function takes all of the HTML code on your page and sends it to the user.Later on your application gets bigger and you want to add new features. You add a library that allows you to generate RSS feeds. This library also uses a function named
output()
to output the final feed.When you call
output()
, how does PHP know whether to use youroutput()
function or the RSS library'soutput()
function? It doesn't. Unless you're using namespaces.Example
How do we solve having two
output()
functions? Simple. We stick eachoutput()
function in its own namespace.That would look something like this:
Later when we want to use the different functions, we'd use:
Or we can declare that we're in one of the namespaces and then we can just call that namespace's
output()
:No Namespaces?
If we didn't have namespaces we'd have to (potentially) change a lot of code any time we added a library, or come up with tedious prefixes to make our function names unique. With namespaces, we can avoid the headache of naming collisions when mixing third-party code with our own projects.
Namespace is like packaging many things into a single pack. Imagine a namespace as a drawer in which you can put all kinds of things: a pencil, a ruler, a piece of paper and so forth. To avoid using each other's items, you decide to label the drawers so it's clear what belongs to whom.