zoukankan      html  css  js  c++  java
  • 命名空间

    定义命名空间 ¶

    (PHP 5 >= 5.3.0, PHP 7)

    虽然任意合法的PHP代码都可以包含在命名空间中,但只有以下类型的代码受命名空间的影响,它们是:类(包括抽象类和traits)、接口、函数和常量。

    命名空间通过关键字namespace 来声明。如果一个文件中包含命名空间,它必须在其它所有代码之前声明命名空间,除了一个以外:declare关键字。

    Example #1 声明单个命名空间

    <?php
    namespace MyProject;

    const CONNECT_OK = 1;
    class Connection { /* ... */ }
    function connect() { /* ... */  }

    ?>

    在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句。另外,所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前:

    Example #2 声明单个命名空间

    <html>
    <?php
    namespace MyProject; // 致命错误 - 命名空间必须是程序脚本的第一条语句
    ?>

    另外,与PHP其它的语言特征不同,同一个命名空间可以定义在多个文件中,即允许将同一个命名空间的内容分割存放在不同的文件中。

     

    使用命名空间:基础 ¶

    (PHP 5 >= 5.3.0, PHP 7)

    在讨论如何使用命名空间之前,必须了解 PHP 是如何知道要使用哪一个命名空间中的元素的。可以将 PHP 命名空间与文件系统作一个简单的类比。在文件系统中访问一个文件有三种方式:

    1. 相对文件名形式如foo.txt。它会被解析为 currentdirectory/foo.txt,其中 currentdirectory 表示当前目录。因此如果当前目录是 /home/foo,则该文件名被解析为/home/foo/foo.txt
    2. 相对路径名形式如subdirectory/foo.txt。它会被解析为 currentdirectory/subdirectory/foo.txt
    3. 绝对路径名形式如/main/foo.txt。它会被解析为/main/foo.txt
    PHP 命名空间中的元素使用同样的原理。例如,类名可以通过三种方式引用:
    1. 非限定名称,或不包含前缀的类名称,例如 $a=new foo(); 或 foo::staticmethod();。如果当前命名空间是currentnamespace,foo 将被解析为 currentnamespacefoo。如果使用 foo 的代码是全局的,不包含在任何命名空间中的代码,则 foo 会被解析为foo。 警告:如果命名空间中的函数或常量未定义,则该非限定的函数名称或常量名称会被解析为全局函数名称或常量名称。详情参见 使用命名空间:后备全局函数名称/常量名称
    2. 限定名称,或包含前缀的名称,例如 $a = new subnamespacefoo(); 或 subnamespacefoo::staticmethod();。如果当前的命名空间是 currentnamespace,则 foo 会被解析为 currentnamespacesubnamespacefoo。如果使用 foo 的代码是全局的,不包含在任何命名空间中的代码,foo 会被解析为subnamespacefoo
    3. 完全限定名称,或包含了全局前缀操作符的名称,例如, $a = new currentnamespacefoo(); 或currentnamespacefoo::staticmethod();。在这种情况下,foo 总是被解析为代码中的文字名(literal name)currentnamespacefoo

    下面是一个使用这三种方式的实例:

    file1.php

    <?php
    namespace FooBarsubnamespace;

    const FOO = 1;
    function foo() {}
    class foo
    {
        static function staticmethod() {}
    }
    ?>

    file2.php

    <?php
    namespace FooBar;
    include 'file1.php';

    const FOO = 2;
    function foo() {}
    class foo
    {
        static function staticmethod() {}
    }

    /* 非限定名称 */
    foo(); // 解析为 FooBarfoo resolves to function FooBarfoo
    foo::staticmethod(); // 解析为类 FooBarfoo的静态方法staticmethod。resolves to class FooBarfoo, method staticmethod
    echo FOO; // resolves to constant FooBarFOO

    /* 限定名称 */
    subnamespacefoo(); // 解析为函数 FooBarsubnamespacefoo
    subnamespacefoo::staticmethod(); // 解析为类 FooBarsubnamespacefoo,
                                      // 以及类的方法 staticmethod
    echo subnamespaceFOO; // 解析为常量 FooBarsubnamespaceFOO
                                      
    /* 完全限定名称 */
    FooBarfoo(); // 解析为函数 FooBarfoo
    FooBarfoo::staticmethod(); // 解析为类 FooBarfoo, 以及类的方法 staticmethod
    echo FooBarFOO; // 解析为常量 FooBarFOO
    ?>

    注意访问任意全局类、函数或常量,都可以使用完全限定名称,例如 strlen() 或 Exception 或 INI_ALL

    Example #1 在命名空间内部访问全局类、函数和常量

    <?php
    namespace Foo;

    function strlen() {}
    const INI_ALL = 3;
    class Exception {}

    $a = strlen('hi'); // 调用全局函数strlen
    $b = INI_ALL; // 访问全局常量 INI_ALL
    $c = new Exception('error'); // 实例化全局类 Exception
    ?>
    add a note add a note

     

    使用命名空间:别名/导入 ¶

    (PHP 5 >= 5.3.0, PHP 7)

    允许通过别名引用或导入外部的完全限定名称,是命名空间的一个重要特征。这有点类似于在类 unix 文件系统中可以创建对其它的文件或目录的符号连接。

    所有支持命名空间的PHP版本支持三种别名或导入方式:为类名称使用别名、为接口使用别名或为命名空间名称使用别名。PHP 5.6开始允许导入函数或常量或者为它们设置别名。

    在PHP中,别名是通过操作符 use 来实现的. 下面是一个使用所有可能的五种导入方式的例子:

    Example #1 使用use操作符导入/使用别名

    <?php
    namespace foo;
    use MyFullClassname as Another;

    // 下面的例子与 use MyFullNSname as NSname 相同
    use MyFullNSname;

    // 导入一个全局类
    use ArrayObject;

    // importing a function (PHP 5.6+)
    use function MyFullfunctionName;

    // aliasing a function (PHP 5.6+)
    use function MyFullfunctionName as func;

    // importing a constant (PHP 5.6+)
    use const MyFullCONSTANT;

    $obj = new namespaceAnother; // 实例化 fooAnother 对象
    $obj = new Another; // 实例化 MyFullClassname 对象
    NSnamesubnsfunc(); // 调用函数 MyFullNSnamesubnsfunc
    $a = new ArrayObject(array(1)); // 实例化 ArrayObject 对象
    // 如果不使用 "use ArrayObject" ,则实例化一个 fooArrayObject 对象
    func(); // calls function MyFullfunctionName
    echo CONSTANT; // echoes the value of MyFullCONSTANT
    ?>

    注意对命名空间中的名称(包含命名空间分隔符的完全限定名称如 FooBar以及相对的不包含命名空间分隔符的全局名称如 FooBar)来说,前导的反斜杠是不必要的也不推荐的,因为导入的名称必须是完全限定的,不会根据当前的命名空间作相对解析。

    为了简化操作,PHP还支持在一行中使用多个use语句

    Example #2 通过use操作符导入/使用别名,一行中包含多个use语句

    <?php
    use MyFullClassname as Another, MyFullNSname;

    $obj = new Another; // 实例化 MyFullClassname 对象
    NSnamesubnsfunc(); // 调用函数 MyFullNSnamesubnsfunc
    ?>

    导入操作是在编译执行的,但动态的类名称、函数名称或常量名称则不是。

    Example #3 导入和动态名称

    <?php
    use MyFullClassname as Another, MyFullNSname;

    $obj = new Another; // 实例化一个 MyFullClassname 对象
    $a = 'Another';
    $obj = new $a;      // 实际化一个 Another 对象
    ?>

    另外,导入操作只影响非限定名称和限定名称。完全限定名称由于是确定的,故不受导入的影响。

    Example #4 导入和完全限定名称

    <?php
    use MyFullClassname as Another, MyFullNSname;

    $obj = new Another; // instantiates object of class MyFullClassname
    $obj = new Another; // instantiates object of class Another
    $obj = new Another hing; // instantiates object of class MyFullClassname hing
    $obj = new Another hing; // instantiates object of class Another hing
    ?>

     

  • 相关阅读:
    java学习笔记3
    java学习笔记 2
    linux用户登录指定目录
    [Jenkins 新插件] 兼容阿里开发手册 (P3C) 的火线插件安装使用教程
    Color a Tree HDU
    网络流初步
    K度限制MST poj 1639
    曼哈顿距离MST
    ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
    HDU Distinct Values
  • 原文地址:https://www.cnblogs.com/clphp/p/5130706.html
Copyright © 2011-2022 走看看