php: get variable type hint using reflection
Try:
<?php
class Expense {
/**
* @var int
*/
private $id;
}
$refClass = new ReflectionClass('Expense');
foreach ($refClass->getProperties() as $refProperty) {
if (preg_match('/@var\s+([^\s]+)/', $refProperty->getDocComment(), $matches)) {
list(, $type) = $matches;
var_dump($type);
}
}
Output:
string(3) "int"
For PHP 7.4
$reflection = new \ReflectionProperty('className', 'propertyName');
echo $reflection->getType()->getName();