很多开源系统如osCommerce框架中,都会在其源码中找到use这个关键字,如osCommerce框架中就在index.PHP文件中出现了这段源码:
PHP;">
use osCommerce\OM\Core\Autoloader;
use osCommerce\OM\Core\OSCOM;
@H_404_3@
其实,PHP的use关键字是自PHP5.3以上版本引入的。它的作用是给一个外部引用起别名。这是命名空间的一个重要特性,它同基于unix的文件系统的为文件或目录创建连接标志相类似。
1、为一个类取别名
2、为一个接口取别名
3、为一个命名空间取别名
这三种方式都是用 use 关键字来完成。下面是三种别名的分别举例: //Example #1 importing/aliasing with the use operator
PHP;">
@H_404_22@
@H_404_3@
注意的一点是,
PHP也可以在同一行上申明多个,等同于上面的写法
还有值得一说的是,
这里由于给变量$a 赋值了 'Another',编译的时候,就将$a 定位到 Classname 了。PHP;">
$obj=newAnother;//instantiatesobjectofclassMy\Full\Classname
NSname\subns\func();//callsfunctionMy\Full\NSname\subns\func
?>
@H_404_3@
PHP;">
$obj=newAnother;//instantiatesobjectofclassMy\Full\Classname
$a = 'Another';
$obj = New $a; // instantiates object of class Another
?>
@H_404_3@