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

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

WBOY
WBOYOriginal
2016-06-06 20:10:321259browse

例如2个类文件

<code>a.php
namespace A;
class Test1{
}

b.php
namespace B;
class Test2{

}
</code>

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

<code>include('a.php');
$c = new \A\Test1()
</code>

还是直接

<code>use A;
$c = new \A\Test1()
</code>

回复内容:

例如2个类文件

<code>a.php
namespace A;
class Test1{
}

b.php
namespace B;
class Test2{

}
</code>

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

<code>include('a.php');
$c = new \A\Test1()
</code>

还是直接

<code>use A;
$c = new \A\Test1()
</code>

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

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

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

spl-autoload-register

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

<code>include('a.php');
use A\Test1;
$c = new Test1();</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn