什么是PHP名称空间?

大体上什么是名称空间?

一个外行的回答和一个例子将是伟大的。


当前回答

在其他编程语言中有一些技术,比如名称空间(比如Java中的包)。它们被用来在一个项目中拥有多个具有相同名称的类。

来自php文档(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.

其他回答

在其他编程语言中有一些技术,比如名称空间(比如Java中的包)。它们被用来在一个项目中拥有多个具有相同名称的类。

来自php文档(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.

命名空间允许您将一堆代码放在一个名称下,并且不会与类、函数和常量发生任何命名冲突。

它允许您的代码驻留在该名称空间中。

PHP使用有点争议的字符\来显示名称空间级别。人们很愤怒,因为它也被用作转义字。

要在PHP中使用名称空间,请在文件顶部使用如下内容。

namespace my\namespace;

您可以在关于名称空间的官方PHP文档中找到更多信息。

命名空间对函数和类的作用就像作用域对变量的作用一样。它允许您在同一程序的不同部分使用相同的函数或类名,而不会引起名称冲突。

简单地说,可以将名称空间看作一个人的姓氏。如果有两个人叫“约翰”,你可以用他们的姓氏来区分他们。

的场景

假设您编写了一个使用名为output()的函数的应用程序。output()函数获取页面上的所有HTML代码并将其发送给用户。

稍后,应用程序变得更大,您希望添加新功能。您添加了一个允许生成RSS提要的库。这个库还使用一个名为output()的函数来输出最终提要。

当您调用output()时,PHP如何知道是使用您的output()函数还是RSS库的output()函数?它不是。除非您正在使用名称空间。

例子

我们如何解决有两个output()函数?简单。我们将每个output()函数置于其自己的名称空间中。

它看起来是这样的:

namespace MyProject;

function output() {
    # Output HTML page
    echo 'HTML!';
}

namespace RSSLibrary;

function output(){
    # Output RSS feed
    echo 'RSS!';
}

稍后,当我们想要使用不同的函数时,我们可以使用:

\MyProject\output();
\RSSLibrary\output();

或者我们可以声明我们在其中一个命名空间中,然后我们可以调用该命名空间的output():

namespace MyProject;

output(); # Output HTML page
\RSSLibrary\output();

没有命名空间?

如果没有命名空间,每次添加库时就必须(潜在地)修改大量代码,或者提出冗长的前缀以使函数名惟一。使用名称空间,我们可以避免在将第三方代码与自己的项目混合时产生命名冲突的麻烦。

命名空间的工作方式类似于目录。您知道如何将文件放在与父目录(或任何其他目录)中的文件名称相同的目录中吗?好吧,命名空间让你在应用程序中为变量、函数和类做到这一点。

不久前,PHP中出现了一种趋势,即大量的静态函数类。调用这些函数的唯一方法是在调用前加上类名。这是对名称空间的一种原始尝试,但没有多少好处。

命名空间基本上允许您将代码放入容器中。这将防止使用相同名称的两个函数(以及类和变量)出现问题。

这在大型应用程序中非常有用,可以防止代码段共享相同名称的问题。

例如,假设我们需要两个名为“TheMessage”的函数。这两者都将分别打印(回显)不同的消息。 通常,这将导致语法错误,因为您不能有两个具有相同名称的函数。

要解决这个问题,可以将这些函数放到单独的名称空间中。这将允许您使用两个函数而不会出现任何错误。