Here's a nasty one. If you create an instance of a class and use register_shutdown_function to clean up the class at the end of the script, does calling unset on the object call the shutdown function? I'm not sure it does, but can't tell because I don't know if its synchronous or asynchronous. Here's what I did to try to test it: testclass.php: class TestClass { function TestClass() { register_shutdown_function(array(&$this, "End")); } function End() { echo "Ending\n"; } } Code (markup): testunset.php: include("testclass.php"); for ( $i = 0; $i < 10; $i++ ) { $test = new TestClass(); echo "Before\n"; unset($test); echo "After\n"; } Code (markup): Output: Before After Before After Before After Before After Before After Before After Before After Before After Before After Before After Ending Ending Ending Ending Ending Ending Ending Ending Ending Ending Code (markup): I know what you're thinking (maybe). Why not just call the shutdown function before I call unset? #1: I'm hoping that PHP is clever enough to do this for me. #2: I don't want to get errors at the end of the script if PHP actually does call the shutdown function AGAIN at the end. Make sense?
Yes it's calling the shutdown function. The output to the browser is just delayed until after your for loop. From http://www.php.net/manual/en/function.register-shutdown-function.php
exam, Only problem I see with that is, I'm running it at commandline. Not through browser. SO, is it calling the shutdown function when I call unset? or is it happening AFTER the script ends? I can't tell from the output. Do you have any specific proof that its calling it when I call unset? The blurb you quoted does not say anything about the shutdown function being called when I call unset, only that it gets called after the request is complete. Thanks.
Try putting exit ("Dead!\n"); in your End() function instead of the echo statement. That should make it pretty obvious. If it works as it *should* the output will be: Before Dead!
Got this: Before After Before After Before After Before After Before After Before After Before After Before After Before After Before After Death!
Well, it would appear that either unset is not actually destroying the object, or the object's shutdown function doesn't get called until the script exists, which IMO would be a bug and eliminates the use of a shutdown function as a deconstructor. (Sometimes I wish php would be a little more like C) BTW, what version of php are you using?
I'm with you on PHP being more like C++. I'm using version 4.3.11 This hasn't even been reported as a bug. Probably because the documentation says that it gets called when the script ends. Oh well.
I'd like to see if it still behaves like this in newer versions of php (>= 4.4.0). I'd report it as a bug, or at least as a feature request.