2
<?php

class Test {

    public function foo() {
        echo "Foo!";
        die();
    }

    public function __destruct() {
        header('location: http://google.com');
    }

}

$Test = new Test;
$Test->foo();

I have a class which simply assign's various Session variables then pings the user back to their previous script. I figured that it would be simpler to have a single redirect on the destruct instead of 10 lines saying the same thing.

I later found a bug in my script and found out that it was nigh on unkillable. For future reference, is there anyway to kill this script or will it always redirect?

To clarify, the script always redirects even with die(), exit in the foo function.

ComputerUser
  • 4,828
  • 15
  • 58
  • 71
  • 1
    Related: http://stackoverflow.com/questions/236795/php-destructor-vs-register-shutdown-function/236830#236830 – Pekka Nov 22 '10 at 12:25

1 Answers1

1

$Test object is destructed when a browser disconnects: it won't redirect just because the browser won't receive the header! However, if you explicitly unset($Test);, it will

UPD: I'm wrong: the script will call all destructors with the browser still connected. However, I wouldn't rely on that

kolypto
  • 31,774
  • 17
  • 105
  • 99
  • The script always redirects. I would have thought the expected action above would be for it to half after printing Foo – ComputerUser Nov 22 '10 at 12:29
  • @JasonS: The redirect can still occur if output buffering is turned on. There's a [setting](http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering) for that in php.ini; maybe that's on here? – Michael Madsen Nov 22 '10 at 12:34