什么是PHP名称空间?

大体上什么是名称空间?

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


当前回答

就像目录和文件一样,PHP中的命名空间用于对类、函数、接口和常量进行分组。

例子:

Filesystem      |   PHP Namespace
----------------|------------------
/Dir/File.txt   |  \Namespace\Class

它提供了一种包装来自全局空间的项的方法,并允许使用普通项名而不会在程序中引起名称冲突。 PHP 5.3.0和PHP 7支持它。

但是PHP命名空间与基于Unix的文件系统的类比存在一定的局限性:

                          | Filesystem            |        PHP Namespace
--------------------------|-----------------------|-------------------------
Cas sensitive             |       No              |        Yes                
--------------------------|-----------------------|-------------------------
Name with plain number    |       Yes             |        No
--------------------------|-----------------------|-------------------------
Path level                |       Yes             |        Yes   
--------------------------|-----------------------|-------------------------
Plain metacharacters name |       Yes             |        No        

该原理扩展到编程词中的命名空间。

其他回答

在其他编程语言中有一些技术,比如名称空间(比如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中的命名空间用于对类、函数、接口和常量进行分组。

例子:

Filesystem      |   PHP Namespace
----------------|------------------
/Dir/File.txt   |  \Namespace\Class

它提供了一种包装来自全局空间的项的方法,并允许使用普通项名而不会在程序中引起名称冲突。 PHP 5.3.0和PHP 7支持它。

但是PHP命名空间与基于Unix的文件系统的类比存在一定的局限性:

                          | Filesystem            |        PHP Namespace
--------------------------|-----------------------|-------------------------
Cas sensitive             |       No              |        Yes                
--------------------------|-----------------------|-------------------------
Name with plain number    |       Yes             |        No
--------------------------|-----------------------|-------------------------
Path level                |       Yes             |        Yes   
--------------------------|-----------------------|-------------------------
Plain metacharacters name |       Yes             |        No        

该原理扩展到编程词中的命名空间。

由于通过了解“命名空间”更容易了解关键字“使用”,所以让我首先通过一个基本的Laravel项目来解释命名空间。

有一个名为:controller .php的控制器类,它位于以下路径: app/Http/Controllers从项目的根目录 还有另一个名为controller .php的控制器类,但是这个类在路径中: vendor/laravel/framework/src/Illuminate/Routing来自项目根目录

如果您是php新手,您还不需要查看源代码,因为它可能会使您感到困惑,相反,让我向您解释 它是我们所关心的,并将帮助我们理解“名称空间”和“使用”。

事实上,第一个控制器类 app/Http/Controllers/Controller.php需要使用 第二个控制器类vendor/laravel/framework/src/Illuminate/Routing/ controller .php。 它实际上需要扩展这个类,以便能够访问它的内容来处理一些关键的路由功能。

那么一个类如何扩展具有相同名称的另一个类呢? 类Controller扩展Controller?这是行不通的, 除非有办法区分这两个类,这时namespace和use关键字就派上用场了 有助于完成的任务,允许使用;(类;方法;接口和常量),具有相同的名称,在相同的作用域中。

now how is it done in the code? very simple! if we look at app/Http/Controllers/Controller.php source code, we can see at the top of the class namespace is declared as: namespace App\Http\Controllers, so this is how you give your class a namespace so it can be referenced by other classes now this looks the same as the path to this class from the project’s root directory, with little difference and that’s the usage of “\” instead of “/“ (same as command prompt in windows), but there is another difference and that’s the App with capital ‘A’ in the namespace versus ‘app’ with Lowercase ‘a’ in the path. Also note that namespace is case-sensitive.

所以名称空间和路径是不同的概念,它可以遵循路径结构,如果有帮助的话但它不需要是到 类,方法,接口或常量,例如看看: vendor/laravel/framework/src/Illuminate/Routing/Controller.php源代码

在类的顶部,我们看到命名空间被声明为:Illuminate\Routing

现在让我们来看看“使用”关键字, 我们使用" use "关键字使我们的类意识到我们想在类中使用的特定类或函数

so we are not importing or including anything we’re just letting our class know that we will be using a specific class or method by referencing them by their namespace let’s take a look at app/Http/Controllers/Controller.php source code, as you can see from the line: “use Illuminate\Routing\Controller as BaseController”, the “use” keyword followed by namespace for the target class (note that Illuminate\Routing\Controller.php and Illuminate\Routing\Controller ‘without .php extension’ are interchangeable)

我们可以使用“as”关键字和“use”关键字为特定的类、方法、接口或常量指定一个别名 app/Http/Controllers/Controller.php扩展Illuminate\Routing\Controller.php为BaseController: “类控制器扩展BaseController”。

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

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

命名空间是控制程序中名称的简单系统。 它确保名称是唯一的,不会导致任何冲突。