How to get property class namespace in php 5.6 via reflection -


i want namespace of class property through reflectionclass.

namespace foo\bar; use foo\quux\b;  class a{     /**    * @var b $b    */    protected $b = null;     ...  } 

in class i'm creating reflectionclass object , trying namespace of property b create reflectionclass it's class.

$namespace = "foo\\bar"; $classname = "a"; $rc = new \reflectionclass("$namespace\\$classname"); $type = getvartype($rc->getproperty("b")->getdoccomment()); // returns b $ns = $rc->getproperty("b")-> ... // want find out namespace of class b $rc1 = new \reflectionclass("$ns\\$type"); 

is there possibility find out namespaces class uses? pair namespaces class a using discovered property type.

i know can solve problem type hinting this: @var foo\quux\b $b, have many classes class a many properties , methods using types b , don't want refactor whole code.

is possible find namespaces use statement or have use explicit type hinting?

thanks!

you should able use getnamespacename find out, getproperty can tell well

namespace foo;  class {     protected $something; }  namespace bar;  class b {     protected $a;      public function __construct(){         $this->a = new \foo\a();     } }  $reflection = new \reflectionclass('bar\\b'); $a = $reflection->getproperty("a"); var_dump($a); 

you'll get

object(reflectionproperty)#2 (2) {
["name"]=> string(1) "a"
["class"]=> string(5) "bar\b"
}


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -