Jump to content

How to capture fatal errors in php script?


xingfang

Recommended Posts

Hello,recently, the company needed an PHP script to capture error,i try to use “set_error_handler”,but it can’t capture fatal error,like redeclare a function,then I try to change a way that use “register_shutdown_function”,but I found it that also unable to capture above mentioned errors at the end of the script.

my php version is 7.0.1

Link to comment
Share on other sites

First off: Why do you want to capture fatal errors? When the application code is fudged up, there's obviously nothing you could do about that at runtime. Or do you just want to display a user-friendly error message instead of the infamous blank page? Then you should set up a custom error page in the webserver configuration.

 

You cannot catch compile-time errors (like a duplicate declaration) by registering an error handler at runtime, because that code never gets executed. It's theoretically possible to register the handler before the script is compiled (e. g. through an auto-prependend file). But there's no reason to do that except for very exotic scenarios.

Edited by Jacques1
Link to comment
Share on other sites

I agree with Jacques, for things like redeclaring a function you should be either using a custom page (or catching them in testing). I'm wondering if you could set display_errors(0) in your page or php.ini file, and catch things in your code with something like this (obviously this will vary depending on what the error you want to catch is and what your expected behaviour is).

if(function())
{
//your usual code

}
else
{
some error

}

This is risky though, because as well as hiding the error you want, you're hiding other errors in the page (or the whole site if you use the ini file)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.