William Entriken
William Entriken

Reputation: 39283

How to make an "Undefined property" error a fatal error and kill the script?

Currently I get an email whenever there is a PHP error in our production application. That works great.

Today a query failed and now I am seeing thousands of "undefined property" errors (all from one page load). This issue is cascading into a problem with sending email because so many error emails are coming to me.

I would like to configure PHP so that "Undefined property" is a fatal error and the script fails at that point. Is that possible?

Upvotes: 2

Views: 471

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 76621

Yes, it's possible. Check out the die or exit command. Assuming that you have a single function where you send the email based on the exception, you can modify it so just after sending the email, an if would check whether your criteria to exit the script is fulfilled and if so, call die or exit.

However, I would recommend that instead of crashing the script in this case the errors should enter into a queue of messages, which, when the script ends would be sent as an error digest.

Upvotes: 0

delboy1978uk
delboy1978uk

Reputation: 12375

Make an error_handler:

function errorHandler($severity, $message, $file, $line) {
    throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");

Check the docs here https://www.php.net/manual/en/function.set-error-handler.php

Upvotes: 1

Related Questions