update page now
Laravel Live Japan

ReflectionProperty::getType

(PHP 7 >= 7.4.0, PHP 8)

ReflectionProperty::getTypeObtiene el tipo de una propiedad

Descripción

public ReflectionProperty::getType(): ?ReflectionType

Obtiene el tipo asociado a una propiedad.

Parámetros

Esta función no contiene ningún parámetro.

Valores devueltos

Devuelve una ReflectionType si la propiedad tiene un tipo, y null en caso contrario.

Ejemplos

Ejemplo #1 Ejemplo de ReflectionProperty::getType()

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

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

El ejemplo anterior mostrará:

string

Ver también

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