You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.1 KiB
54 lines
1.1 KiB
<?php
|
|
//命名空间a
|
|
namespace a
|
|
{
|
|
const const_a=1; //常量a
|
|
|
|
function func_a()
|
|
{
|
|
echo __FUNCTION__.'<br>';
|
|
}
|
|
|
|
class cls_a
|
|
{
|
|
static function func()
|
|
{
|
|
echo __CLASS__.'--'.__FUNCTION__.'<br>';
|
|
}
|
|
}
|
|
}
|
|
|
|
//命名空间b
|
|
namespace b
|
|
{
|
|
//导入类
|
|
use a\cls_a as cls_b; //导入命名空间a\b\c的类cls_a,设置别名cls_a
|
|
|
|
$obj=new cls_b;
|
|
$obj -> func();
|
|
|
|
//导入函数
|
|
use function a\func_a;
|
|
func_a();
|
|
|
|
//导入常量
|
|
use const a\const_a;
|
|
echo const_a.'<br>';
|
|
}
|
|
|
|
namespace c
|
|
{
|
|
//导入命名空间
|
|
use a as aa;
|
|
|
|
echo aa\const_a.'<br>';;
|
|
|
|
aa\func_a();
|
|
|
|
$obj=new aa\cls_a();
|
|
$obj -> func();
|
|
}
|
|
|
|
//感悟:命名空间就像在一个文件中把其中一筐代码框住,放在一个空间中,当要使用是就用use导入
|
|
//注意:导入不同文件的命名空间需要用iclude'file'
|
|
?>
|