0

While implementing some class I've run into a little problem:

If the script ends and destructors are called because the script has finished, I wanted to trigger an error occasionally.

I thought the trigger_error() function would be of use. However, if error_reporting(-1) the triggered error is not send any longer to STDOUT or STDERR - while it is expected to do so (e.g. if not within the __destructor/termination phase of the script, trigger_error works as expected).

If I echo some message out, it will be send to STDOUT (CLI mode).

I now wonder

  1. how I can trigger an error in this phase of the application?
  2. and/or alternatively how can I detect that currently the script is shutting down because it has successfully ended?

Note: I tested connection_status() but it's useless in my case as it's about connection handling only and merely unrelated. I wonder if there is some function that does the same for the scripts execution status (starting, running, exiting).

Simplified Example Code

This is some very reduced example code to illustrate the issue. Naturally is the error only triggered if it makes sense for the object:

<?php
class TriggerTest
{
    public function __destruct()
    {
        trigger_error('You should have missed something.');
    }
}
$obj = new TriggerTest;
exit();

The problem is, that trigger_error() gets executed but the error does not appear anywhere.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • raising an error in a destructor is not usually considered as a "good" practice. – ALOToverflow Jun 03 '11 at 12:53
  • @Francis: is it not? How come? – chelmertz Jun 03 '11 at 13:15
  • @chelmertz look at the 2nd note about the destructor : http://php.net/manual/en/language.oop5.decon.php – ALOToverflow Jun 03 '11 at 13:29
  • @Francis: do you mean "Attempting to throw an exception from a destructor [...]"? The question does not mention an exception being thrown, rather a warning/error which does not halt the script. – chelmertz Jun 03 '11 at 13:39
  • @Francis: That note is about throwing an exception. My question is about triggering an error - and if not possible - how to determine if the script is in such a state. – hakre Jun 03 '11 at 13:40

2 Answers2

1

How about if you force the error reporting to be a certain setting, trigger the error and then set the error reporting back to it's normal form?

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
0

Answer: Just do it. I had some misconfiguration for the error handler and therefore it did not work. My fault.

However it's still interesting if there is any function or similar to determine the execution state on shutdown.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836