1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

register_shutdown_function and unset

Discussion in 'PHP' started by durango, Oct 18, 2005.

  1. #1
    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?
     
    durango, Oct 18, 2005 IP
    exam likes this.
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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, Oct 18, 2005 IP
  3. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    durango, Oct 18, 2005 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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!
     
    exam, Oct 18, 2005 IP
  5. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Got this:
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Before
    After
    Death!
     
    durango, Oct 18, 2005 IP
  6. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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?
     
    exam, Oct 19, 2005 IP
  7. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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.
     
    durango, Oct 19, 2005 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    exam, Oct 19, 2005 IP
    durango likes this.