(PHP 8 >= 8.2.0)
此屬性用於標記敏感參數,如果該參數存在於堆疊追蹤中,則應將其值遮蓋。
<?php
function defaultBehavior(
string $secret,
string $normal
) {
throw new Exception('Error!');
}
function sensitiveParametersWithAttribute(
#[\SensitiveParameter]
string $secret,
string $normal
) {
throw new Exception('Error!');
}
try {
defaultBehavior('password', 'normal');
} catch (Exception $e) {
echo $e, PHP_EOL, PHP_EOL;
}
try {
sensitiveParametersWithAttribute('password', 'normal');
} catch (Exception $e) {
echo $e, PHP_EOL, PHP_EOL;
}
?>
上述範例在 PHP 8.2 中的輸出類似於
Exception: Error! in example.php:7 Stack trace: #0 example.php(19): defaultBehavior('password', 'normal') #1 {main} Exception: Error! in example.php:15 Stack trace: #0 example.php(25): sensitiveParametersWithAttribute(Object(SensitiveParameterValue), 'normal') #1 {main}