时间:2021-07-01 10:21:17 帮助过:63人阅读
- <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>