当前位置:Gxlcms > PHP教程 > php框架-php的命名空间使用是否省去了include和require的作用

php框架-php的命名空间使用是否省去了include和require的作用

时间:2021-07-01 10:21:17 帮助过:63人阅读

例如2个类文件

a.php
namespace A;
class Test1{
}

b.php
namespace B;
class Test2{

}

那么在c.php里怎么写呢?是写

include('a.php');
$c = new \A\Test1()

还是直接

use A;
$c = new \A\Test1()

回复内容:

例如2个类文件

a.php
namespace A;
class Test1{
}

b.php
namespace B;
class Test2{

}

那么在c.php里怎么写呢?是写

include('a.php');
$c = new \A\Test1()

还是直接

use A;
$c = new \A\Test1()

没有,继续要include 只是降低了不同文件中 new xxxxx 里面 xxxx 命名冲突的可能性

命名空间只是解决了重名问题,同样需要引入文件。当然也可以使用composer来进行包管理

楼主,看下psr-4和psr-0。
文件的加载和命名空间是两回事。不使用命名空间一样可以实现自动加载。
关于自动加载可以看下:

spl-autoload-register

两者根本不是一个概念。include是引入文件,命名空间是封装,对类分类
c.php写法:

include('a.php');
use A\Test1;
$c = new Test1();

人气教程排行