php8.5
Home/ Manual/ reflection / reflectionproperty/ ReflectionProperty::getType

ReflectionProperty::getType

PHP function Edit on GitHub ✎

(PHP 7 >= 7.4.0, PHP 8)

Gets a property's type

Description

ReflectionProperty::getType(): ReflectionType|null

Gets the associated type of a property.

Parameters Parameters

Return Values

Returns a ReflectionType if the property has a type, and null otherwise.

Examples

ReflectionProperty::getType example

php
<?php
class User
{
    public string $name;
}

$rp = new ReflectionProperty('User', 'name');
echo $rp->getType()->getName();
?>

The above example will output:

output
string

See Also

  • ReflectionProperty::hasType
  • ReflectionProperty::isInitialized

Source: reference/reflection/reflectionproperty/gettype.xml · from the official PHP manual (php/doc-en)