1

Here's what I want to do just if a fatal error is in the file I don't want the script to halt execution

try { 
require "somethin.php"
} catch(...) {}

Would some method of file_get_contents() and eval() be a way around

Raidri
  • 17,258
  • 9
  • 62
  • 65
y2k
  • 65,388
  • 27
  • 61
  • 86
  • Why didn't you try it first? – samayo Jun 11 '13 at 22:31
  • 3
    In fairness he probably did try it, haha did you catch that? – Dave Chen Jun 11 '13 at 22:35
  • @DaveChen You are saying "probably" because, he did not give any hints whether if he did/or not. So, who is the actaully [censored] here?hmm. Sadly, I read his disgusting comment. – samayo Jun 11 '13 at 22:38
  • For me, I did try it, http://puu.sh/3dF22.png and it still erred, so that leads the conclusion that he did try it before posting. – Dave Chen Jun 11 '13 at 22:39
  • @DaveChen So for every question asked in SO with the OP failing to specify whether he tried/or not. We are supposed to try it out for them by ourselves? hmm.. interesting. – samayo Jun 11 '13 at 22:44
  • Yes! I was going to assume that the code worked fine, but it clearly didn't. Unless you're clear that the code works without testing it yourself, don't ask if they've tried it. :) – Dave Chen Jun 11 '13 at 22:46
  • A fatal error is like the word suggests fatal. There is no *sane* way to try to recover from this. – PeeHaa Jun 11 '13 at 22:46
  • Why don't you use `include` rather than `require`, and register an exception error handler? Require is explicitly designed to throw a fatal error. Include does the same but with a warning. –  Oct 14 '15 at 07:24

2 Answers2

6

Notice - this does not answer the poster's question. The ugly reality is you cannot recover from a fatal error but this works for other errors

require doesn't throw an exception but rather triggers an error by default.

To have php throw exceptions you have to register an error exception handler.

You can find an example here

But basically it

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

With this in play it's possible to catch and deal with the error as you see fit

Orangepill
  • 24,500
  • 3
  • 42
  • 63
  • I'd like to mention that it's probably better to check if the file can be read before requiring it, no? – Dave Chen Jun 11 '13 at 22:34
  • 1
    @DaveChen That's one approach ... I like this better because it's only one hit on the filesystem – Orangepill Jun 11 '13 at 22:35
  • 1
    It's also worth noting that this will cause almost all php errors to throw exceptions (a good thing in my opinion) except for parse time errors – Orangepill Jun 11 '13 at 22:40
  • 1
    like syntax errors? Having all PHP errors/warnings throw exceptions, oh gosh it's like Java all over again. – Dave Chen Jun 11 '13 at 22:41
  • 1
    This doesn't catch fatal errors (which really shouldn't be catched in the first place). – PeeHaa Jun 11 '13 at 22:45
  • Depends on what kind of fatal error... again parse errors it wont because the exception handler can't be installed if the file in question can't be parsed. – Orangepill Jun 11 '13 at 22:47
  • but those errors should be able to be easily found in development. – Orangepill Jun 11 '13 at 22:48
  • 1
    No a fatal error is a fatal error. Which is what this question is about. It is both in the title and the question itself. As such this doesn't answer the question. – PeeHaa Jun 11 '13 at 22:48
  • The bad thing is that this causes E_NOTICE and the like to become fatal unless you rig you error handler conditionally throw if the error is of a certain type. – Orangepill Jun 11 '13 at 22:50
  • 2
    @PeeHaa埽 Follow the link, read the docs. This deals with fatal errors – Orangepill Jun 11 '13 at 22:51
  • No it really doesn't deal with fatal errors. It handles warnings / notices, but not fatal errors: http://codepad.org/4nzzYQSG – PeeHaa Jun 11 '13 at 22:53
  • @Orangepill - 1) it doesn't inherently make anything fatal - it makes things catchable (if you don't catch the exception, then it's fatal) 2) don't know about you, but when my developers put something out to production one of the aims is to have no E_NOTICE and no E_WARNING at all, so this wouldn't be a problem anyway. – HorusKol Jun 11 '13 at 22:56
  • @HorusKol I agree but I thought it should be stated. After doing this stuff that would have squeaked by with notices would now fall down. – Orangepill Jun 11 '13 at 22:57
  • 1
    @Orangepill - and my thought on that is "good" ;) - I've elevated the error reporting on a number of legacy scripts before now, and got rid of all sorts of deprecated stuff like ereg, and bad practices like uninitiated variables - if the final script is better written at the end of it (even if it means more work), what's the problem? – HorusKol Jun 11 '13 at 23:00
  • @PeeHaa埽 Point to you on this one... – Orangepill Jun 11 '13 at 23:02
  • Yay moar intarnet points! :-) Thanks for undeleting the answer and adding the disclaimer. – PeeHaa Jun 12 '13 at 07:34
5

No there is really no sane way to catch (uncatchable ;-) ) fatal errors in PHP. So there is also no sane way to recover from this. Which makes sense because it is a fatal error.

Basically what PHP is saying is: this is something you cannot / should not try to recover from.

Note that also @Orangepill's solution won't work for fatal errors: http://codepad.org/4nzzYQSG

Now if you would want to do something when a fatal error happens (e.g. send an email to you to inform you of this) you may want to look into: https://stackoverflow.com/a/2146171/508666

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • +1 My solution below while useful won't even throw something given the example above. – Orangepill Jun 11 '13 at 23:05
  • +1 for making me proud. (even though, I don't know what you guys are talking about :P ) – samayo Jun 11 '13 at 23:06
  • So what is the right thing to do ... nix my answer or just but a banner across the top stating it doesn't answer the question? – Orangepill Jun 11 '13 at 23:08
  • @Orangepill Close it, as not a real question – samayo Jun 11 '13 at 23:08
  • nah the question is good... there just is not an answer the the op wants. – Orangepill Jun 11 '13 at 23:11
  • @Orangepill It may still be useful to someone coming across this question. So I would 1+ the banner idea if it is clear you cannot catch fatal errors, but at least recover from some gracefully. – PeeHaa Jun 11 '13 at 23:11