1、防止引用文件中函数名相同,导致函数重定义错误:
//test1.php
<?php
namespace foo;
function func(){
echo "test1/func";
}
//test.php
<?php
include 'test1.php';
function func(){
echo "test/func";
}
func(); /*输出test/func*/
foo\func(); /*输出test1/func*/
2、防止引用文件中类名相同,导致类重定义错误:
//test3.php
<?php
namespace foo;
class A{
static function func(){
echo "test3/A";
}
}
//test4.php
<?php
include 'test3.php';
class A{
static public function func(){
echo "test4/A";
}
}
A::func(); /*test4/A*/
foo\A::func(); /*test3/A*/
use foo\A as FA;
FA::func(); /*test3/A*/