ReflectionProperty::getDocComment

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

ReflectionProperty::getDocComment — Récupère le commentaire de documentation d'une propriété

Description

public function ReflectionProperty::getDocComment(): string |false

Récupère le commentaire de documentation d'une propriété.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Le commentaire de documentation s'il existe, sinon false .

Exemples

Exemple #1 Exemple avec ReflectionProperty::getDocComment()

<?php
class Str
{
 /**
 * @var int La taille de la chaîne de caractère
 */
 public $length = 5;
}
$prop = new ReflectionProperty('Str', 'length');
var_dump($prop->getDocComment());
?>

Résultat de l'exemple ci-dessus est similaire à :

string(66) "/**
 * @var int La taille de la chaîne de caractère
 */"

Exemple #2 Déclarations de propriétés multiples

Si plusieurs déclarations de propriétés sont précédées d'un unique commentaire de documentation, le commentaire de documentation ne fait référence uniquement à la première propriété.

<?php
class Foo
{
 /** @var string */
 public $a, $b;
}
$class = new \ReflectionClass('Foo');
foreach ($class->getProperties() as $property) {
 echo $property->getName() . ': ' . var_export($property->getDocComment(), true) . PHP_EOL;
}
?>

L'exemple ci-dessus va afficher :

a: '/** @var string */'
b: false

Voir aussi

Found A Problem?

Learn How To Improve This Page • Submit a Pull Request • Report a Bug
+add a note

User Contributed Notes 1 note

up
2
Jim
4 years ago
Unfortunately, inherited doc comments are not supported.
<?php
class A {
 /**
 * @var string
 */
 public string $prop = 'A';
}
class B extends A {
 public string $prop = 'B';
}
$prop = new ReflectionProperty('B', 'prop');
var_dump($prop->getDocComment());
?>

results in FALSE
+add a note

AltStyle によって変換されたページ (->オリジナル) /