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