This question was asked in June 2021, long before the release of PHP 8.2 (Dec 2022). For PHP 8.2+ see Josh's answer. For PHP <8.2, no answer solves the question (including my own). The code I posted in my answer is the closest to a solution that fits my needs.
Consider the following code. In the event that an exception occurs, the trace (which will be logged and stored in a database) will include the sensitive password
data. How can sensitive data in cases like this, while allowing other non-sensitive arguments, be hidden?
<?php
$user = 'john';
$pass = 'secret';
function auth($user, $pass) {
// authentication logic
}
function login($user, $pass) {
throw new Exception('Unexpected error');
// various logic
auth($user, $pass);
// various logic
}
try {
login($user, $pass);
} catch (Throwable $e) {
send_to_log($e->getTrace()); // This reveals the password "secret"
}