類別 User
{
/**
* @var string
*/
public $name;
}
函式 getTypeNameFromAnnotation(字串 $className, 字串 $propertyName): ?字串
{
$rp = new \ReflectionProperty($className, $propertyName);
如果 (preg_match('/@var\s+([^\s]+)/', $rp->getDocComment(), $matches)) {
返回 $matches[1];
}
返回 null;
}
echo getTypeNameFromAnnotation('User', 'name');
// string