0

According to the book I am reading, printing object that does not have __toString() implemented should throw an error:

Object of class popp\ch04\batch22\Person could not be converted to string ...

When I try to do this, I am getting a message Process finished with exit code 255, but no descriptive error. I am using PhpStorm.

<?php

    class Person{}

    $person = new Person();

    try {
        print $person;
    } catch(Exception $e) {
        print $e;
    }

Is there a switch I should enable to receive errors?

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
sanjihan
  • 5,592
  • 11
  • 54
  • 119
  • Have you disabled PHP error messages/logging? – Qirel Nov 30 '19 at 19:31
  • There is ` – sanjihan Nov 30 '19 at 19:33
  • 1
    Most likely `error_reporting()` is set to none, try adding `error_reporting(E_ALL);` at the top of your file (after ` – Qirel Nov 30 '19 at 19:34
  • 1
    Please try to put this line at the beginnig of your file: `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);`. – N'Bayramberdiyev Nov 30 '19 at 19:36
  • That said, errors should not be displayed in production environments (just in development). They should always be logged though. So `ini_set('display_errors', 1);` is just for development, `ini_set('display_errors', 0);` is for production. `error_reporting(E_ALL);` should always be set though. – Qirel Nov 30 '19 at 19:38
  • Thanks @N'Bayramberdiyev and Qirel. That did the trick. – sanjihan Nov 30 '19 at 19:41
  • Now the error is displayed, but not caught. Is this the type of error that cannot get caught? – sanjihan Nov 30 '19 at 19:43
  • Its not an exception, so no. Its a fatal error, which means you can implement something with `register_shutdown_function()` - but not through an exception. – Qirel Nov 30 '19 at 19:46
  • It's discussed in [this thread](https://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error#answer-2146171). – N'Bayramberdiyev Nov 30 '19 at 19:48

1 Answers1

4

You can configure PHP to generate errors by itself instead of adding error reporting code to PHP operators manually.

error_reporting should always be set to E_ALL.

Development:

display_errors should be set to 1.

Production:

display_errors should be set to 0.

log_errors should be set to 1.

Read more about error reporting.

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
  • Great read! The author says `the use of @ operator` is bad practice. What does `@` do? I googled the list of PHP operators, but haven't found anything about `@` at all. – sanjihan Dec 01 '19 at 07:57
  • 1
    It suppresses errors. You can read [here](https://www.php.net/manual/en/language.operators.errorcontrol.php). – N'Bayramberdiyev Dec 01 '19 at 08:00
  • Thanks man! At https://phpdelusions.net/articles/error_reporting#code it says that the error handling code should be included in bootstrap file. I am not sure what is the usual way of telling PHP to `include` a file in files. Is this something that can be done in `php.ini` or do you need to rely on some coding principles? – sanjihan Dec 01 '19 at 08:20
  • If you have a custom error handler, it should be included in your bootstrap file which refers to _the process of loading the environment a program (or a script, in the case of PHP) needs to operate._ ([source](https://www.binpress.com/php-bootstrapping-crash-course/)). If you let PHP handle errors, configuration values above should be set in `php.ini`. – N'Bayramberdiyev Dec 01 '19 at 08:36
  • And you may want to read about [include](https://www.php.net/manual/en/function.include.php) and [require](https://www.php.net/manual/en/function.require.php). – N'Bayramberdiyev Dec 01 '19 at 08:38