命名空间
命名空间的作用和解决的问题
1、用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/常量之间的名字冲突。
2、为很长的标识符名称(通常是为了缓解第一类问题而定义的)创建一个别名(或简短)的名称,提高源代码的可读性。
注意:命名空间并没有引入文件的功能
a\index.php
<?php
namespace a;
class Index{
public function getdesc(){
return '我是a文件夹下的index.php文件';
}
}
?>
b\index.php
<?php
namespace b;
class Index{
public function getdesc(){
return '我是b文件夹下的index.php文件';
}
}
?>
index.php
<?php
// include __DIR__ . '\a\index.php';
// include __DIR__ . '\b\index.php';
spl_autoload_register('autoload',true,true);
function autoload($className){
include __DIR__.DIRECTORY_SEPARATOR.strtolower($className).'.php';
}
use a\Index;
$obj = new Index();
// 我是a文件夹下的index.php文件
echo $obj->getdesc();
echo "<br/>";
use b\Index as Bindex;
$obj = new Bindex();
// 我是b文件夹下的index.php文件
echo $obj->getdesc();
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: