ReflectionParameter::getType

(PHP 7, PHP 8)

ReflectionParameter::getTypeGets a parameter's type

Beschreibung

public ReflectionParameter::getType(): ?ReflectionType

Gets the associated type of a parameter.

Parameter-Liste

Diese Funktion besitzt keine Parameter.

Rückgabewerte

Returns a ReflectionType object if a parameter type is specified, null otherwise.

Beispiele

Beispiel #1 ReflectionParameter::getType() Usage as of PHP 7.1.0

As of PHP 7.1.0, ReflectionType::__toString() is deprecated, and ReflectionParameter::getType() may return an instance of ReflectionNamedType. To get the name of the parameter type, ReflectionNamedType() is available in this case.

<?php
function someFunction(int $param$param2) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams $reflectionFunc->getParameters();
$reflectionType1 $reflectionParams[0]->getType();
$reflectionType2 $reflectionParams[1]->getType();

assert($reflectionType1 instanceof ReflectionNamedType);
echo 
$reflectionType1->getName(), PHP_EOL;
var_dump($reflectionType2);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

int
NULL

Beispiel #2 ReflectionParameter::getType() Usage before PHP 7.1.0

<?php
function someFunction(int $param$param2) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams $reflectionFunc->getParameters();
$reflectionType1 $reflectionParams[0]->getType();
$reflectionType2 $reflectionParams[1]->getType();

echo 
$reflectionType1PHP_EOL;
var_dump($reflectionType2);
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe mit PHP 7.0:

int
NULL

Beispiel #3 ReflectionParameter::getType() Usage in PHP 8.0.0 and later

As of PHP 8.0.0, this method may return a ReflectionNamedType instance or a ReflectionUnionType instance. The latter is a collection of the former. To analyze a type, it is often convenient to normalize it to an array of ReflectionNamedType objects. The following function will return an array of 0 or more ReflectionNamedType instances.

<?php
function getAllTypes(ReflectionParameter $reflectionParameter): array
{
    
$reflectionType $reflectionParameter->getType();

    if (!
$reflectionType) return [];

    return 
$reflectionType instanceof ReflectionUnionType
        
$reflectionType->getTypes()
        : [
$reflectionType];
}
?>

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Kategorie: Software & Web-Development

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

u4gm Jackie Robinson Day Tips for MLB The Show 26

Jackie Robinson Day always hits a little differently in MLB The Show 26. It's not just another timed drop with a few themed items and a pile of mi ...

Geschrieben von jhb66 am 20.04.2026 10:54:57
Forum: HTML, JavaScript, AJAX, jQuery, CSS, Bootstrap, LESS
U4GM What Makes POE 2 Plus 3 Spell Amulet Crafting Work

Most caster players hit the same wall sooner or later: the amulet slot starts deciding how far the build can really go. Damage, utility, even how ...

Geschrieben von jhb66 am 20.04.2026 10:54:13
Forum: PHP Developer Forum
u4gm How to Time Crafted Gear in WoW Midnight

When Midnight arrives, loads of players will sprint straight to crafting and chase the biggest item level they can afford. That usually looks smar ...

Geschrieben von jhb66 am 20.04.2026 10:53:23
Forum: News / Kostenloses
u4gm Why Clean Racing Wins More in Forza Horizon 6

Ranked multiplayer in Horizon isn't really about hero moments. It's more about staying tidy while everyone else loses their head, and that's why a ...

Geschrieben von jhb66 am 20.04.2026 10:52:47
Forum: Fragen/Vorschläge zum Forum