开发者

PHP how to trigger user error with trigger_error in an object destructor while the script shuts down?

While implementing some class I've run into a lit开发者_开发知识库tle 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.


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜