遺憾的是,不支援繼承的文件註釋。
<?php
類別 A {
/**
* @var string
*/
public string $prop = 'A';
}
類別 B 延伸 A {
public string $prop = 'B';
}
$prop = new ReflectionProperty('B', 'prop');
var_dump($prop->getDocComment());
?>
結果為 FALSE
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
ReflectionProperty::getDocComment — 取得屬性的文件註釋
此函式沒有參數。
如果存在文件註釋,則返回該註釋;否則返回 false
。
範例 #1 ReflectionProperty::getDocComment() 範例
<?php
class Str
{
/**
* @var int 字串的長度
*/
public $length = 5;
}
$prop = new ReflectionProperty('Str', 'length');
var_dump($prop->getDocComment());
?>
上述範例的輸出類似於:
string(53) "/** * @var int The length of the string */"
範例 #2 多個屬性宣告
如果多個屬性宣告前面只有一個文件註釋,則該註釋僅指第一個屬性。
<?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;
}
?>
上述範例將輸出:
a: '/** @var string */' b: false