Vivendi
Vivendi

Reputation: 21097

Get type of parameter with Reflection

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?

Upvotes: 0

Views: 941

Answers (2)

nice ass
nice ass

Reputation: 16729

// 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.

Upvotes: 0

w00
w00

Reputation: 26812

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.

Upvotes: 2

Related Questions