php命名空间的使用,同一个命名空间可以在多个文件中定义


php namespace的使用,直接打印出已经定义的命名空间

直接上代码,a.php , b.php, c.php , main.php

a.php

 <?php
 namespace A{
     class Person{
         public $name = 'ljl';
     }
 }

b.php

 <?php
 namespace A{
     class Animal{
         public $name = 'dog';
     }
 }
                                     

c.php

 <?php
 namespace A\Test;
 class Test{
     public $name = 'test';
 }
                                      
                                      

main.php

 <?php
 include "./a.php";
 include "./b.php";
 include "./c.php";
 $a = new \A\Person();
 var_dump($a);
 
 $animal = new \A\Animal();
 var_dump($animal);
 
 
 $namespaces=array();
 foreach(get_declared_classes() as $name) {
     if(preg_match_all("@[^\\\]+(?=\\\)@iU", $name, $matches)) {
         $matches = $matches[0];
         $parent =&$namespaces;
         while(count($matches)) {
             $match = array_shift($matches);
             if(!isset($parent[$match]) && count($matches))
                 $parent[$match] = array();
             $parent =&$parent[$match];
 
         }
     }
 }
 
 print_r($namespaces);
                                                                                           
                                                                                           
                                                                                           
                                                                                           

  

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM