What are namespaces?

What are PHP Namespaces?

What are Namespaces in general?

A Layman answer with an example would be great.


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:

MyProjectoutput();
RSSLibraryoutput();

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
RSSLibraryoutput();

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.


A namespace allows you to place a bunch of code under a name and not have any naming conflicts with classes, functions and constants.

It allows your code to live in that namespace.

PHP uses the somewhat controversial character to show namespace levels. People got up in arms because it is also used as an escape character.

To use the namespace in PHP, use something like this at the top of your file.

namespace mynamespace;

You can find a lot more information on the official PHP documentation for namespaces.


There are techniques like namespaces in other programming languages (like packages in Java). They are used to be able to have mutliple classes with the same name wihtin a project.

From the php documentation (http://www.php.net/manual/en/language.namespaces.rationale.php):

What are namespaces? In the broadest definition namespaces are a way of encapsulating items. This can be seen as an abstract concept in many places. For example, in any operating system directories serve to group related files, and act as a namespace for the files within them. As a concrete example, the file foo.txt can exist in both directory /home/greg and in /home/other, but two copies of foo.txt cannot co-exist in the same directory. In addition, to access the foo.txt file outside of the /home/greg directory, we must prepend the directory name to the file name using the directory separator to get /home/greg/foo.txt. This same principle extends to namespaces in the programming world.

链接地址: http://www.djcxy.com/p/9970.html

上一篇: PHP语法$ var1是什么?

下一篇: 什么是命名空间?