好好爱自己!

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);
                                                                                           
                                                                                           
                                                                                           
                                                                                           

  

 

 

posted @ 2019-01-10 10:39  立志做一个好的程序员  阅读(1752)  评论(0编辑  收藏  举报

不断学习创作,与自己快乐相处