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