什么是PHP名称空间?

大体上什么是名称空间?

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


当前回答

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

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

其他回答

由于通过了解“命名空间”更容易了解关键字“使用”,所以让我首先通过一个基本的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”。

Namespace就像将许多东西打包到一个包中。将命名空间想象成一个抽屉,您可以在其中放各种东西:一支铅笔、一把尺子、一张纸等等。为了避免使用彼此的物品,你们决定在抽屉上贴上标签,这样就能清楚地知道什么东西属于谁。

您可以使用命名空间来避免您创建的代码与内部PHP类/函数/常量或第三方类/函数/常量之间的名称冲突。 命名空间还能够别名(或缩短)Extra_Long_Names,旨在减少第一个问题,提高源代码的可读性。

我们都知道,名称空间和特征在PHP中并不新鲜,但仍然有许多PHP开发人员因为它们的复杂性而不使用这些伟大的概念。 所以,在这篇文章中。我会用例子来解释清楚。 什么是名称空间和特征?

如何在代码中实现它们以使代码可重用和可扩展?

名称空间的好处

您可以使用命名空间来避免您创建的代码与内部PHP类/函数/常量或第三方类/函数/常量之间的名称冲突。

命名空间还能够别名(或缩短)Extra_Long_Names,旨在减少第一个问题,提高源代码的可读性。 让我们通过一个例子来理解名称空间。 在htdocs(xampp)或WWW (xwamp)中创建名为“php_oops”的文件夹 在根目录下创建一个名为“namespaces”的新文件夹,然后在namespaces文件夹下创建一个文件index.php。

<?php
// FilePath:- namespaces/index.php

// let's say, we have two classes in index,
// here, these two classes have global space
class A 
{ 
   function __construct()
   { 
      echo "I am at Global space, Class A";
   }
}
class B
{
   function __construct()
   {
      echo "I am at Global space, Class B";
   }
}
// now create an object of class and 
$object = new A; // unqualified class name
echo "<br/>";
$object = new \B; // fully qualified class name
// output: 
I am at Global space, Class A
I am at Global space, Class B

参考, https://medium.com/@akgarg007/php-laravel-namespaces-and-traits-01-9540fe2969cb

就像目录和文件一样,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        

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

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