函数名称:ReflectionAttribute::getArguments()
适用版本:PHP 8.0.0 及以上版本
函数说明:ReflectionAttribute::getArguments() 方法用于获取属性的参数值。当属性具有参数时,可以使用该方法获取这些参数的值。
用法示例:
class ExampleClass
{
#[AttributeWithArguments('value1', 'value2')]
public $exampleProperty;
}
$reflectionClass = new ReflectionClass('ExampleClass');
$reflectionProperty = $reflectionClass->getProperty('exampleProperty');
$attributes = $reflectionProperty->getAttributes();
foreach ($attributes as $attribute) {
$attributeInstance = $attribute->newInstance();
$arguments = $attribute->getArguments();
echo 'Attribute: ' . $attribute->getName() . PHP_EOL;
echo 'Arguments: ';
foreach ($arguments as $argument) {
echo $argument . ' ';
}
echo PHP_EOL;
}
解释示例代码:
输出示例:
Attribute: AttributeWithArguments
Arguments: value1 value2
注意事项: