PHP 中命名空间的作用

来源:互联网 发布:数字病理 人工智能 编辑:程序博客网 时间:2024/06/06 02:38

php中命名空间 其实就是为了避免类名冲突

main.class.php

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. namespace App\main;  
  2. class aa {  
  3.     public static function test() {  
  4.         echo 1;  
  5.     }  
  6. }  
test.class.php
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. namespace App\test;  
  2. class aa {  
  3.     public static function test() {  
  4.         echo 2;  
  5.     }  
  6. }  

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. require 'main.class.php';  
  3. require 'test.class.php';  
  4. App\main\aa::test();  
  5. App\test\aa::test();  

2个类名都是aa ,当在一个文件里调用的时候,通过命名空间,就不会报错。
0 0
原创粉丝点击