php 手册use,PHP 中的use关键字

今天看osCommerce源码框架时index.php文件中用到了use这个 关键字 , use osCommerce\OM\Core\Autoloader; use osCommerce\OM\Core\OSCOM; 不是很清楚,特地翻了一下,use 关键字 是php5.3以上版本引入的 它的作用是给一个外部引用起别名。这是命名空间的一个

今天看osCommerce源码框架时index.php文件中用到了use这个关键字,

use osCommerce\OM\Core\Autoloader;

use osCommerce\OM\Core\OSCOM;

不是很清楚,特地翻了一下,use关键字是php5.3以上版本引入的

它的作用是给一个外部引用起别名。这是命名空间的一个重要特性,它同基于unix的文件系统的为文件或目录创建连接标志相类似。

PHP命名空间支持三种别名方式(或者说引用):

1:为一个类取别名

2:为一个接口取别名

3:为一个命名空间取别名

这三种方式都是用 use 关键字来完成。下面是三种别名的分别举例:

//Example #1 importing/aliasing with the use operator

namespacefoo;

useMy\Full\ClassnameasAnother;// this is the same as use My\Full\NSname as NSnameuseMy\Full\NSname;// importing a global classuseArrayObject;$obj= new namespace\Another;// instantiates object of class foo\Another$obj= newAnother;// instantiates object of class My\Full\ClassnameNSname\subns\func();// calls function My\Full\NSname\subns\func$a= newArrayObject(array(1));// instantiates object of class ArrayObject

// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject

?>

注意的一点是,对于已命名的名字,全称就包含了分隔符,比如 Foo\Bar,而不能用FooBar,而“\Foo\Bar”这个头部的"\"是没必要的,也不建议这样写。引入名必须是全称,并且跟当前命名空间没有程序上的关联。

PHP也可以在同一行上申明多个,等同于上面的写法

useMy\Full\Classname asAnother,My\Full\NSname;$obj = newAnother;// instantiates object of class My\Full\ClassnameNSname\subns\func();// calls function My\Full\NSname\subns\func?>

还有值得一说的是,引入是在编译时执行的,因此,别名不会影响动态类,例如:

useMy\Full\Classname asAnother,My\Full\NSname;$obj = newAnother;// instantiates object of class My\Full\Classname

$a = 'Another';

$obj = New $a; // instantiates object of class Another

?>

这里由于给变量$a 赋值了 'Another',编译的时候,就将$a 定位到 Classname 了。

文档中还有更详细的说明,篇幅有限,就说到这...

详细参考:http://php.net/manual/en/language.namespaces.importing.php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值