is it possible to get list of defined namespaces

前端 未结 2 900
后悔当初
后悔当初 2020-12-13 06:29

HI there,

I was wondering if there is a way in php 5.3+ to get a list of defined namespaces within an application. so

if file 1 has namespace F

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 07:22

    Firstly, to see if a class exists, used class_exists.

    Secondly, you can get a list of classes with namespace using get_declared_classes.

    In the simplest case, you can use this to find a matching namespace from all declared class names:

    function namespaceExists($namespace) {
        $namespace .= "\\";
        foreach(get_declared_classes() as $name)
            if(strpos($name, $namespace) === 0) return true;
        return false;
    }
    

    Another example, the following script produces a hierarchical array structure of declared namespaces:

    Gives:

    Array
    (
        [FirstNamespace] => 
        [SecondNamespace] => Array
            (
                [FirstSubNamespace] => 
            )
        [ThirdNamespace] => Array
            (
                [FirstSubNamespace] => 
                [SecondSubNamespace] => 
    
            )
    )
    

提交回复
热议问题