命名空间
在线手册:中文 英文
PHP手册

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

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

PHP 命名空间支持 有两种使用别名或导入方式:为类名称使用别名,或为命名空间名称使用别名。注意PHP不支持导入函数或常量。

在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;

$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 对象
?>
注意对命名空间中的名称(包含命名空间分隔符的完全限定名称如 Foo\Bar以及相对的不包含命名空间分隔符的全局名称如 FooBar)来说,前导的反斜杠是不必要的也不允许有反斜杠,因为导入的名称必须是完全限定的,不会根据当前的命名空间作相对解析。 Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not allowed, as import names must be fully qualified, and are not processed relative to the current namespace.

为了简化操作,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
?>

导入操作是在编译执行的,但动态的类名称、函数名称或常量名称则不是。 Importing is performed at compile-time, and so does not affect dynamic class, function or constant names.

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
?>


命名空间
在线手册:中文 英文
PHP手册
PHP手册 - N: 使用命名空间:别名/导入

用户评论:

c dot 1 at smithies dot org (14-Aug-2011 11:26)

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.

Jan Tvrdk (11-Jan-2011 02:26)

Importing and aliasing an interface name is also supported.

thinice at gmail.com (02-Dec-2010 06:07)

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();
?>

nsdhami at live dot jp (15-Jul-2010 10:01)

The "use" keyword can not be declared inside the function or method. It should be declared as global, after the "namespace" as:

<?php

namespace mydir;

// works perfectly
use mydir/subdir/Class1 as Class1;

function
fun1()
{
   
// Parse error: syntax error, unexpected T_USE
   
use mydir/subdir/Class1 as Class1;
}

class
Class2
{
    public function
fun2()
    {
       
// Parse error: syntax error, unexpected T_USE
       
use mydir/subdir/Class1 as Class1;
    }
}
?>