<object id="xwuag"></object>
      1. <tbody id="xwuag"><pre id="xwuag"><video id="xwuag"></video></pre></tbody>
        <progress id="xwuag"></progress>

        <nav id="xwuag"><big id="xwuag"><rt id="xwuag"></rt></big></nav>
        <em id="xwuag"></em>

        <nav id="xwuag"></nav><rp id="xwuag"></rp>
        <li id="xwuag"></li>
      2. <rp id="xwuag"></rp>
        <s id="xwuag"></s><th id="xwuag"><pre id="xwuag"><video id="xwuag"></video></pre></th>
      3. <nav id="xwuag"><center id="xwuag"></center></nav>
          <dd id="xwuag"><big id="xwuag"></big></dd>

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

          (PHP 5 >= 5.3.0, PHP 7)

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

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

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

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

          <?php
          namespace foo;
          use 
          My\Full\Classname as Another;

          // 下面的例子与 use My\Full\NSname as NSname 相同
          use My\Full\NSname;

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

          // importing a function (PHP 5.6+)
          use function My\Full\functionName;

          // aliasing a function (PHP 5.6+)
          use function My\Full\functionName as func;

          // importing a constant (PHP 5.6+)
          use const My\Full\CONSTANT;

          $obj = new namespace\Another// 实例化 foo\Another 对象
          $obj = new Another// 实例化 My\Full\Classname 对象
          NSname\subns\func(); // 调用函数 My\Full\NSname\subns\func
          $a = new ArrayObject(array(1)); // 实例化 ArrayObject 对象
          // 如果不使用 "use \ArrayObject" ,则实例化一个 foo\ArrayObject 对象
          func(); // calls function My\Full\functionName
          echo CONSTANT// echoes the value of My\Full\CONSTANT
          ?>
          注意对命名空间中的名称(包含命名空间分隔符的完全限定名称如 Foo\Bar以及相对的不包含命名空间分隔符的全局名称如 FooBar)来说,前导的反斜杠是不必要的也不推荐的,因为导入的名称必须是完全限定的,不会根据当前的命名空间作相对解析。

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

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

          <?php
          use My\Full\Classname as AnotherMy\Full\NSname;

          $obj = new Another// 实例化 My\Full\Classname 对象
          NSname\subns\func(); // 调用函数 My\Full\NSname\subns\func
          ?>

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

          Example #3 导入和动态名称

          <?php
          use My\Full\Classname as AnotherMy\Full\NSname;

          $obj = new Another// 实例化一个 My\Full\Classname 对象
          $a 'Another';
          $obj = new $a;      // 实际化一个 Another 对象
          ?>

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

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

          <?php
          use My\Full\Classname as AnotherMy\Full\NSname;

          $obj = new Another// instantiates object of class My\Full\Classname
          $obj = new \Another// instantiates object of class Another
          $obj = new Another\thing// instantiates object of class My\Full\Classname\thing
          $obj = new \Another\thing// instantiates object of class Another\thing
          ?>

          Scoping rules for importing

          The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped. The following example will show an illegal use of the use keyword:

          Example #5 Illegal importing rule

          <?php
          namespace Languages;

          class 
          Greenlandic
          {
              use 
          Languages\Danish;

              ...
          }
          ?>

          Note:

          Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

          add a note add a note

          User Contributed Notes 16 notes

          up
          82
          dominic_mayers at yahoo dot com
          2 years ago
          The keyword "use" has been recycled for three distinct applications:
          1- to import/alias classes, traits, constants, etc. in namespaces,
          2- to insert traits in classes,
          3- to inherit variables in closures.
          This page is only about the first application: importing/aliasing. Traits can be inserted in classes, but this is different from importing a trait in a namespace, which cannot be done in a block scope, as pointed out in example 5. This can be confusing, especially since all searches for the keyword "use" are directed to the documentation here on importing/aliasing.
          up
          69
          anon
          5 years ago
          The <?php use ?> statement does not load the class file. You have to do this with the <?php require ?> statement or by using an autoload function.
          up
          64
          k at webnfo dot com
          6 years ago
          Note that you can not alias global namespace:

          use \ as test;

          echo test\strlen('');

          won't work.
          up
          15
          Mawia HL
          1 year ago
          Here is a handy way of importing classes, functions and conts using a single use keyword:

          <?php
          use Mizo\Web\ {
            
          Php\WebSite,
            
          Php\KeyWord,
            
          Php\UnicodePrint,
            
          JS\JavaScript,
             function
          JS\printTotal,
             function
          JS\printList,
             const
          JS\BUAIKUM,
             const
          JS\MAUTAM
          };
          ?>
          up
          11
          xzero at elite7hackers dot net
          2 years ago
          I couldn't find answer to this question so I tested myself.
          I think it's worth noting:

          <?php
          use ExistingNamespace\NonExsistingClass;
          use
          ExistingNamespace\NonExsistingClass as whatever;
          use
          NonExistingNamespace\NonExsistingClass;
          use
          NonExistingNamespace\NonExsistingClass as whatever;
          ?>

          None of above will actually cause errors unless you actually try to use class you tried to import.

          <?php
          // And this code will issue standard PHP error for non existing class.
          use ExistingNamespace\NonExsistingClass as whatever;
          $whatever = new whatever();
          ?>
          up
          12
          me at ruslanbes dot com
          3 years ago
          Note the code `use ns1\c1` may refer to importing class `c1` from namespace `ns1` as well as importing whole namespace `ns1\c1` or even import both of them in one line. Example:

          <?php
          namespace ns1;

          class
          c1{}

          namespace
          ns1\c1;

          class
          c11{}

          namespace
          main;

          use
          ns1\c1;

          $c1 = new c1();
          $c11 = new c1\c11();

          var_dump($c1); // object(ns1\c1)#1 (0) { }
          var_dump($c11); // object(ns1\c1\c11)#2 (0) { }
          up
          24
          c dot 1 at smithies dot org
          7 years ago
          If you are testing your code at the CLI, note that namespace aliases do not work!

          (Before I go on, all the backslashes in this example are changed to percent signs because I cannot get sensible results to display in the posting preview otherwise. Please mentally translate all percent signs henceforth as backslashes.)

          Suppose you have a class you want to test in myclass.php:

          <?php
          namespace my%space;
          class
          myclass {
          // ...
          }
          ?>

          and you then go into the CLI to test it. You would like to think that this would work, as you type it line by line:

          require 'myclass.php';
          use my%space%myclass; // should set 'myclass' as alias for 'my%space%myclass'
          $x = new myclass; // FATAL ERROR

          I believe that this is because aliases are only resolved at compile time, whereas the CLI simply evaluates statements; so use statements are ineffective in the CLI.

          If you put your test code into test.php:
          <?php
          require 'myclass.php';
          use
          my%space%myclass;
          $x = new myclass;
          //...
          ?>
          it will work fine.

          I hope this reduces the number of prematurely bald people.
          up
          21
          x at d dot a dot r dot k dot REMOVEDOTSANDTHIS dot gray dot org
          6 years ago
          You are allowed to "use" the same resource multiple times as long as it is imported under a different alias at each invocation.

          For example:

          <?php
          use Lend;
          use
          Lend\l1;
          use
          Lend\l1 as l3;
          use
          Lend\l2;
          use
          Lend\l1\Keller;
          use
          Lend\l1\Keller as Stellar;
          use
          Lend\l1\Keller as Zellar;
          use
          Lend\l2\Keller as Dellar;

          ...

          ?>

          In the above example, "Keller", "Stellar", and "Zellar" are all references to "\Lend\l1\Keller", as are "Lend\l1\Keller", "l1\Keller", and "l3\Keller".
          up
          17
          cl
          6 years ago
          Something that is not immediately obvious, particular with PHP 5.3, is that namespace resolutions within an import are not resolved recursively.  i.e.: if you alias an import and then use that alias in another import then this latter import will not be fully resolved with the former import.

          For example:
          use \Controllers as C;
          use C\First;
          use C\Last;

          Both the First and Last namespaces are NOT resolved as \Controllers\First or \Controllers\Last as one might intend.
          up
          4
          ZhangLiang
          2 years ago
          In Chinese,there is an error in translation:
          // 如果不使用 "use \ArrayObject" ,则实例化一个 foo\ArrayObject 对象
          it should be
          // 如果不使用 "use ArrayObject" ,则实例化一个 foo\ArrayObject 对象

          /*********************************************/
          中文下翻译有错误
          // 如果不使用 "use \ArrayObject" ,则实例化一个 foo\ArrayObject 对象
          这句话应该是
          // 如果不使用 "use ArrayObject" ,则实例化一个 foo\ArrayObject 对象
          up
          3
          kelerest123 at gmail dot com
          4 years ago
          For the fifth example (example #5):

          When in block scope, it is not an illegal use of use keyword, because it is used for sharing things with traits.
          up
          2
          ultimater at gmail dot com
          2 years ago
          Note that "use" importing/aliasing only applies to the current namespace block.

          <?php

          namespace SuperCoolLibrary
          {
              class
          Meta
             
          {
                  static public function
          getVersion()
                  {
                      return
          '2.7.1';
                  }
              }
          }

          namespace
          {
              use
          SuperCoolLibrary\Meta;
              echo
          Meta::getVersion();//outputs 2.7.1
          }

          namespace
          {
              echo
          Meta::getVersion();//fatal error
          }

          ?>

          To get the expected behavior, you'd use:
          class_alias('SuperCoolLibrary\Meta','Meta');
          up
          4
          Anonymous
          6 years ago
          The last example on this page shows a possibly incorrect attempt of aliasing, but it is totally correct to import a trait \Languages\Languages\Danish.
          up
          3
          thinice at gmail.com
          8 years ago
          Because imports happen at compile time, there's no polymorphism potential by embedding the use keyword in a conditonal.

          e.g.:

          <?php
          if ($objType == 'canine') {
            use
          Animal\Canine as Beast;
          }
          if (
          $objType == 'bovine') {
            use
          Animal\Bovine as Beast;
          }

          $oBeast = new Beast;
          $oBeast->feed();
          ?>
          up
          -1
          dominic_mayers at yahoo dot com
          2 years ago
          To clarify the distinction between inserting a trait in a class and importing a trait in a namespace, here is an example where we first import and then insert a trait.

          <?php
          namespace ns1;
          trait
          T {
            static
          $a = "In T";
          }

          namespace
          ns2;
          use
          ns1\T; // Importing the name of trait ns1\T  in the namespace ns2
          class C {
            use
          T; // Inserting trait T in the class C, making use of the imported name.


          namespace
          main;
          use
          ns2\C;
          echo
          C::$a; // In T;
          up
          -6
          Joey
          1 year ago
          For those hoping for an easy fix of DRY with {} unfortunately you can't nest it or do anything cool with it. In case you can only have one per use and if you don't have one then the whole use is assumed to be wrapped in brackets. That means if you do have one you can't use , as normal with use!

          Not possible:

              use A\B\C\{
                      D, E,
                      F\{X, Y, Z}
                  },
                  X\Y\Z,
                  H\{
                      I\{
                          Y\Y\Y\Y\Y,
                          Z, H, E
                      },
                      J\{
                          A\{
                              G\H\J
                              B\N
                          },
                          G\H\J
                      }
                  };

          備份地址:http://www.lvesu.com/blog/php/language.namespaces.importing.php

          国产成人午夜福利r在线观看_欧美性受xxxx孕妇_白丝娇喘过膝袜爽短裙调教_jizz jizz xxxx