第十四节--命名空间 -- Classes and Objects in PHP5 [14]/* <?php
namespace core_php:utility
{
class textEngine
{
public function uppercase($text) //大写
{
return(strtoupper($text));
}
}
//make non-OO interface 建立一个非OO的接口
function uppercase($text)
{
$e = new textEngine;
return($e->uppercase($text));
}
}
//test class in namespace 测试命名空间中的类
$e = new core_php:utility::textEngine;
print($e->uppercase("from object") . "<br>");
//test function in namespace 测试命名空间中的函数
print(core_php:utility::uppercase("from function") . "<br>");
//bring class into global namespace 把类导入全局命名空间
import class textEngine from core_php:utility;
$e2 = new textEngine;
?>
Import语句把命名空间中的某个部份导入全局的命名空间. 要导入单一的命名空间的成员,可以指定类型为constant,function或class,接着写上成员的名称; //如import class XXX 如果你想导入某一特定类型的所有成员,你可以用*来代替名称; //如 import constant * 导入所有常量 如果你想导入所有类型的所有成员,用*即可. //如 import * 在成员之后,用from关键字加上命名空间的名称. //如 import class textEngine from core_php:utility; 总之你要写成像import * from myNamespace或 import class textEngine from core_php:utility这样的语句,就像例6.17中那样. 注:本文章为原创文章,版权归文章作者与超越PHP网站所有,未经本站同意,禁止任何商业转载。非盈利网站及个人网站转载请注明出处,谢谢合作! 上一篇:第十五节--Zend引擎的发展 -- Classes and Objects in PHP5 [15] 下一篇:第十二节--类的自动加载 -- Classes and Objects in PHP5 [12] 更多相关文章
|
推荐文章
精彩文章
|