문제

Can I somehow check, if my script was canceled by die() in register_shutdown_function()?

Something like this:

register_shutdown_function('shutdown');

die('Calling die()');

function shutdown()
{
    if (???)
    {
        // Script was canceled by die()
    }
}

NOTE: On my website, I use Smarty. So maybe check, if $smarty->display() was called or something like that?

도움이 되었습니까?

해결책

Kind of... but you won't necessarily like how it has to be done.

Since there is no hook that allows you to check if die was called, you will have to somehow fake it. Assuming that you cannot touch all calls to die, that leaves only one option: set some state that signifies "die was called" by default, and remove that state only at the very end of your script, when you know that you are going to exit without die having been called earlier.

"Set some state" sounds suspiciously like global variables and that should be a last resort, so let's use a constant for state:

register_shutdown_function('shutdown');
if (condition) die('Calling die()');

// since we reached this point, die was not called
define('DIE_NOT_CALLED', true);

function shutdown()
{
    if (!defined('DIE_NOT_CALLED'))
    {
        // Script was canceled by die()
    }
}

See it in action.

다른 팁

I think the only way would be to set a globally accessible variable at the end of your script. You can then check in the shutdown function if this variable has been set. If not, the script was terminated prematurely.

First off, you should avoid die and exit if you're deploying on FastCGId. It kills processes and thus prevents them from being re-used for another run.

If you simply want to check if Smarty has output something or not, you might want to try something like:

MySmarty extends Smarty {
  public static $_has_displayed = false;
  public function display(/* params */) {
    $t = parent::display(/* params */);
    self::$_has_displayed = true;
    return $t;
  }
}

register_shutdown_function('foo');
function foo() {
  if (!MySmarty::$_has_displayed) {
    echo "OMG I failed!";
  }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top