-1

I have method which cast array to object by using

$class = get_class($object);
$methodList = get_class_methods($class);

But now I need had information about expected type of variable too. For example from this method:

public function setFoo(int $foo)
{
}

I need get int too. There is any option to get it?

John Conde
  • 217,595
  • 99
  • 455
  • 496
kris016
  • 147
  • 2
  • 10

1 Answers1

2

You can use Reflection. Specifically ReflectionParameter::getType().

function someFunction(int $param, $param2) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();

assert($reflectionType1 instanceof ReflectionNamedType);
echo $reflectionType1->getName(), PHP_EOL;
var_dump($reflectionType2);

The above example will output:

int
NULL
John Conde
  • 217,595
  • 99
  • 455
  • 496