ReflectionProperty::getType

(PHP 7 >= 7.4.0, PHP 8)

ReflectionProperty::getTypeGets a property's type

Descrizione

public ReflectionProperty::getType(): ?ReflectionType

Gets the associated type of a property.

Elenco dei parametri

Questa funzione non contiene parametri.

Valori restituiti

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

Esempi

Example #1 ReflectionProperty::getType() example

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

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

Il precedente esempio visualizzerà:

string

Vedere anche:

add a note

User Contributed Notes 1 note

up
6
email at dronov dot vg
5 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