- namespace file is actually afraid of the class name coincidence, then use this method to prevent an error
//例如
@a.php文件
Class Same
{
public function a()
{
echo 'a';
}
}
@b.php文件
Class Same
{
public function b()
{
echo 'b';
}
}
________________________
运行index.php文件的时候
require("a.php");
require("b.php");
same();
这时就会报错:
Fatal error: Cannot declare class Same, because the name is already in use in *** on line 3
________________________
所以用namespace就可以避免
namespace App\a;
Class Same
{
public function a()
{
echo 'a';
}
}
namespace App\b;
Class Same
{
public function b()
{
echo 'b';
}
}
这时再运行
require("a.php");
require("b.php");
$a = new App\a\Same();
$a->a();
$b = new App\b\Same();
$b->b();
就不会报错了
2.use action is, at the end of the above block of code, and if so references too long
can be simplified
//使用use操作符,声明类命名空间
use App\a;
require("a.php");
require("b.php");
//原始
$a = new App\a\Same();
$a->a();
//修改后
$a = new Same();
$a->a();
3. However, when the use or coincidence, then, can be distinguished as
4 can be understood namespace namespace is to facilitate the management class and the introduction of a mechanism, we can function similar classes in the same namespace, using when introduced directly into the space on it , simply say namespace is to make a package inside the class
5. keyword namespace followed by that name is not the path! We see a lot of the framework behind namespace have followed the "path" is actually to let us know the location of the file where the namespace file when a lot of us, this name will facilitate our search. namespace and include, require completely different! ! Implementation process framework has helped us automatically injected, so we do not need to include require or we'll be dead headache
Reference: https: //www.cnblogs.com/qq254980080/p/9933107.html