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.

5
  • raising an error in a destructor is not usually considered as a "good" practice. Commented Jun 3, 2011 at 12:53
  • @Francis: is it not? How come? Commented Jun 3, 2011 at 13:15
  • @chelmertz look at the 2nd note about the destructor : php.net/manual/en/language.oop5.decon.php Commented Jun 3, 2011 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. Commented Jun 3, 2011 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. Commented Jun 3, 2011 at 13:40

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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.

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.