0

I am using the following code to get the methods from a class:

$reflector = new \ReflectionClass ( $className );
$methods = $reflector->getMethods(ReflectionMethod::IS_PUBLIC);
print_r ( $methods[0] );

Then all i get from it is the name of the property. But I am also interested in the property type. How can i get that info?

2
  • Are you trying to get the methods's return value type? This is not possible in the current version of reflection
    – nice ass
    Commented Dec 7, 2013 at 14:11
  • @onetrickpony no the type of the methods params. Like function foo (array $arr); I want to know what type $arr is.
    – Vivendi
    Commented Dec 7, 2013 at 14:20

2 Answers 2

2

You can do that by doing:

$params = $methods[0]->getParameters();
$params[0]->getClass()->name;

You can only use getClass()->name if that param is strong typed.

1
  • getClass is deprecated now, any other way? Commented Jan 15, 2021 at 11:28
0
// get the list of parameters
$params = $methods[0]->getParameters();

foreach($params as $param){

  if($param->isArray()){
    // array...

  }else{
    // something else...    

    try{
      $paramClass = $param->getClass();

      if($paramClass !== null){
        // it's a required class ($paramClass->name)
        // note that the class must be loaded at this point
      }

    }catch(\Exception $e){

    }

  }

}

These are the only parameter hints you can detect with reflection.

Not the answer you're looking for? Browse other questions tagged or ask your own question.