3

I need to be able to trigger a fatal error in PHP 7 that is not thrown as an exception.

According to the PHP docs, most of the fatal errors have been converted to exceptions. However, I can't find any docs on which ones were and were not converted.

My Question: What is something I can do in PHP to trigger one of the fatal errors that has not been converted to an exception?

Reason: I am developing a universal error handler for an API based application. I need a test case to prove it will work as I say it does.

8
  • 1
    I dont get your question sorry - you can't catch fatal errors in PHP. If you trigger a fatal error you script will stop executing and thats it. Commented Sep 19, 2016 at 15:55
  • If you can't I need to be able to prove that too... although the docs I have been looking at say you can. Commented Sep 19, 2016 at 15:56
  • Also, you absolutely can catch the ones that are thrown as exceptions. I can already prove that. Commented Sep 19, 2016 at 15:57
  • 2
    try { goto b; } catch(Exception $e) { var_dump($e); } - Yes you can catch Exceptions, but no fatal errors. Commented Sep 19, 2016 at 15:58
  • 1
    I came here looking for the same thing. We handle fatal errors using output buffering, and on PHP < 7 we tested for these by calling Triggering_E_ERROR_for_testing_purposes(). This now triggers an exception, so it would be good to find an example that triggers a true E_ERROR error. Commented Jun 4, 2019 at 10:19

1 Answer 1

1

You can trigger a fatal in a number of ways, calling an undefined function is one such way and pretty simple, e.g.

<?php

foo();
<br />
<b>Fatal error</b>:  Uncaught Error: Call to undefined function foo() in [...][...]:4
Stack trace:
#0 {main}
  thrown in <b>[...][...]</b> on line <b>4</b><br />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.