Dutch PHP Conference 2025 - Call For Papers

ReflectionProperty::getType

(PHP 7 >= 7.4.0, PHP 8)

ReflectionProperty::getTypeRécupère le type d'une propriété

Description

public ReflectionProperty::getType(): ?ReflectionType

Récupère le type associé à une propriété.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Retourne une ReflectionType si la propriété à un type, et null sinon.

Exemples

Exemple #1 Exemple de ReflectionProperty::getType()

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

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

L'exemple ci-dessus va afficher :

string

Voir aussi

add a note

User Contributed Notes 1 note

up
6
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
To Top