Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.4: trigger_error() updates #4063

Merged
merged 3 commits into from
Nov 14, 2024
Merged

Conversation

Girgias
Copy link
Member

@Girgias Girgias commented Nov 13, 2024

Turns out the return type is already correct.

I have no idea for a "good" example, I was going to go for something to use E_USER_DEPRECATED but realised the new Deprecated attribute is better suited for this.

Maybe @TimWolla has opinions on how to format it for a See Also section?

Copy link
Member

@cmb69 cmb69 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

if ($divisor == 0) {
trigger_error("Cannot divide by zero", E_USER_ERROR);
if (is_nan($divisor)) {
trigger_error("Cannot divide by NAN", E_USER_WARNING);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message makes not much sense, given that E_USER_WARNING would not abort, so the division will be carried out. Maybe:

Suggested change
trigger_error("Cannot divide by NAN", E_USER_WARNING);
trigger_error("Division by NAN", E_USER_WARNING);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, finding a good example is hard, because generally speaking there is no good reason to use trigger_error() nowadays. Either use an Exception or use error_log() or another logging facility. But PHP's internal error reporting mechanism which allows for side-effects is not a good idea.

That said, how about this:

$password = $_POST['password'] ?? '';
if ($password === '') {
  trigger_error("Using an empty password is unsafe", E_USER_WARNING);
}
$hash = password_hash($password, PASSWORD_DEFAULT);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require that the error is shown to the user, though.

reference/errorfunc/functions/trigger-error.xml Outdated Show resolved Hide resolved
if ($divisor == 0) {
trigger_error("Cannot divide by zero", E_USER_ERROR);
if (is_nan($divisor)) {
trigger_error("Cannot divide by NAN", E_USER_WARNING);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, finding a good example is hard, because generally speaking there is no good reason to use trigger_error() nowadays. Either use an Exception or use error_log() or another logging facility. But PHP's internal error reporting mechanism which allows for side-effects is not a good idea.

That said, how about this:

$password = $_POST['password'] ?? '';
if ($password === '') {
  trigger_error("Using an empty password is unsafe", E_USER_WARNING);
}
$hash = password_hash($password, PASSWORD_DEFAULT);

@Girgias Girgias merged commit 72b70d7 into php:master Nov 14, 2024
2 checks passed
@Girgias Girgias deleted the trigger-error-8.4 branch November 14, 2024 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants