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

// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;

// importing a global class
use ArrayObject;

$obj = new namespace\Another; // instantiates object of class foo\Another
$obj = new Another; // instantiates object of class My\Full\Classname
NSname\subns\func(); // calls function My\Full\NSname\subns\func
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject
?> 

请帮我解决一下这个。

是什么意思use My\Full\Classname as Another;

4

1 回答 1

1

那是一个别名。每次您将Another其称为(相对)命名空间或类名时,它都会被解析为\My\Full\Classname

$x = new Another;
echo get_class($x); // "\My\Full\Classname"
$y = new Another\Something;
echo get_class($y); // "\My\Full\Classname\Something"

以命名空间分隔符开头的标识符\是全限定名称。如果缺少,则根据当前命名空间和由(按此顺序)定义的别名定义解析标识符(和: 它们始终是完全限定use的标识符除外)。usenamespace

PHP 手册:命名空间

于 2012-06-25T06:02:36.913 回答