ReflectionProperty::getType
(PHP 7 >= 7.4.0, PHP 8)
ReflectionProperty::getType — Gets a property's type
参数
此函数没有参数。
返回值
   Returns a ReflectionType if the property has a type,
   and null otherwise.
  
示例
示例 #1 ReflectionProperty::getType() example
<?php
class User
{
    public string $name;
}
$rp = new ReflectionProperty('User', 'name');
echo $rp->getType()->getName();
?>以上示例会输出:
string
参见
- ReflectionProperty::hasType() - Checks if property has a type
 - ReflectionProperty::isInitialized() - Checks whether a property is initialized
 
  +添加备注
  
用户贡献的备注 1 note
  
  
  email at dronov dot vg ¶
  
 
4 years ago
  class User
{
    /**
     * @var string
     */
    public $name;
}
function getTypeNameFromAnnotation(string $className, string $propertyName): ?string
{
    $rp = new \ReflectionProperty($className, $propertyName);
    if (preg_match('/@var\s+([^\s]+)/', $rp->getDocComment(), $matches)) {
        return $matches[1];
    }
    return null;
}
echo getTypeNameFromAnnotation('User', 'name');
// string备份地址:http://www.lvesu.com/blog/php/reflectionproperty.gettype.php