array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'class.sensitiveparameter.php', 1 => 'SensitiveParameter', ), 'up' => array ( 0 => 'reserved.attributes.php', 1 => '预定义注解', ), 'prev' => array ( 0 => 'returntypewillchange.construct.php', 1 => 'ReturnTypeWillChange::__construct', ), 'next' => array ( 0 => 'sensitiveparameter.construct.php', 1 => 'SensitiveParameter::__construct', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'language/predefined/attributes/sensitiveparameter.xml', ), 'extra_header_links' => array ( 'rel' => 'alternate', 'href' => '/manual/en/feeds/class.sensitiveparameter.atom', 'type' => 'application/atom+xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(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}