SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    193
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    when do destructors run

    I just want to make sure I understand this point. For a while I thought destructors run when the script ends.

    On the PHP manual I read "The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence."

    The first part, "as soon as there are no other references to a particular object" can you tell me if the below code is accurate? But I'm not sure what the second part means "or in any order during the shutdown sequence".

    Code PHP:
    // begin script
    $instantiate = new Test();
     
    $instantiate->firstMethod();
     
    $instantiate->secondCall();
    // no more calls to the $instantiate object so the destructor would run now
     
     
    // other script processing, but no references to $instantiate
     
     
     
    // end script

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,158
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    AFAIK unless you call a
    $instantiate->close();
    or similar, how would PHP know whether or not there would be more calls to it?
    I don't think it has "look ahead" ability.
    So in this case, unless it was passed as an include or a resource it would be when the script ends.

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    I read that as well, its inaccurate. Destructors are called at the end of execution of the script, unless they are unset. But that's just how its running now, bottom line, don't rely on destructors to run when you want them too. If you have something that needs done, set a method to do what you need and call it.

    Not Unsetting:
    PHP Code:
    <?php

    class destruct {
        private 
    $id;

        public function 
    __construct($id) {
            
    $this->id $id;
            print 
    "construct " $this->id "\n";
        }

        public function 
    test() {
            print 
    "testing " $this->id "\n";
        }

        public function 
    __destruct() {
            print 
    "desctruct " $this->id "\n";
        }
    }

    $d1 = new destruct(1);
    $d2 = new destruct(2);

    $d1->test();
    print 
    "some procedural stuff\n";
    $d2->test();
    /*
    construct 1
    construct 2
    testing 1
    some procedural stuff
    testing 2
    desctruct 2
    desctruct 1
    */
    Unsetting:
    PHP Code:
    <?php

    class destruct {
        private 
    $id;

        public function 
    __construct($id) {
            
    $this->id $id;
            print 
    "construct " $this->id "\n";
        }

        public function 
    test() {
            print 
    "testing " $this->id "\n";
        }

        public function 
    __destruct() {
            print 
    "desctruct " $this->id "\n";
        }
    }

    $d1 = new destruct(1);
    $d2 = new destruct(2);

    $d1->test();
    unset(
    $d1);
    print 
    "some procedural stuff\n";
    $d2->test();

    /*
    construct 1
    construct 2
    testing 1
    desctruct 1
    some procedural stuff
    testing 2
    desctruct 2
    */

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    193
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    I read that as well, its inaccurate. Destructors are called at the end of execution of the script, unless they are unset. But that's just how its running now, bottom line, don't rely on destructors to run when you want them too. If you have something that needs done, set a method to do what you need and call it.
    Thanks all!

    So, even though you say "don't rely on destructors to run when you want", they'll still always run, correct? I have a database class and my destructor just closes the connection, i figure that's safe??

  5. #5
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by sessions View Post
    So, even though you say "don't rely on destructors to run when you want", they'll still always run, correct? I have a database class and my destructor just closes the connection, i figure that's safe??
    Yes that should be fine!

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    193
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks.

    Can you please tell me again what you meant by don't rely on them to run when you want? Thanks.

  7. #7
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by sessions View Post
    thanks.

    Can you please tell me again what you meant by don't rely on them to run when you want? Thanks.
    I'm saying if you have something critical you want to happen at a certain point, before starting something else, then it doesn't belong in a destructor. They should be used strictly as cleanup (closing a db connection), and not something like say, updating a record in a db with something (last modified?) after you've completed some tasks on it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •