www.webdeveloper.com
Results 1 to 13 of 13

Thread: [RESOLVED] Class Destructor Method

  1. #1
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063

    resolved [RESOLVED] Class Destructor Method

    Is there any reason to define a destructor method for your classes? I just wrote my first OOP script, and I got to thinking about the __destructor method, but I never bother to get rid of the data in my current procedural scripts unless I'm logging someone out of the system or I need to unset something in order to proceed with the next code.

  2. #2
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Normally it's used to run clean up, for example if you have a class that needs to lock a table while it's there for some reason then you'd unlock the tables on destruction.

    I've got a few examples of where I use them, I've got a lead tracking system, my company buys leads, calls them up, then gets a big cut of the refunds, tracking the session object is easy enough, but various other objects need to be transfered between pages, for this I use a destruct clause, when the page ends the destruct clause runs and the object serialises itself into the database, when the object is created again on the next page it's automatically unserialised for use, once the object is done with I call a save function which turns off the normal destruct function and records it as an actual entry(as opposed to an in progress entry, which are unsearchable and temporary(erased 48 hours after last access to save space etc.)).
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  3. #3
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    Do you think you could maybe give me a code example? (Nothing too complicated, mind you; I'm still new to OOP.)

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Let's suppose you create a class that uses its own MySQL connection to interface with the DB:
    PHP Code:
    <?php
    class Foo
    {
       protected 
    $dbConnx;
       public function 
    __construct($host$user$pass$dbNamd)
       {
          
    $this->dbConnx mysql_connect($host$user$pass);
          if(
    $this->dbConnx == false) {
             throw new 
    Exception("Connection failed: ".mysql_error());
          }
          
    $result mysql_select_db($dbName);
          if(
    $result == false) {
             throw new 
    Exception("DB select failed: ".mysql_error());
          }
       }
       
    //
       // various methods to query DB and such
       //
       /**
        * Destructor to ensure DB connection is closed when object destroyed
        */
       
    public function __destruct()
       {
          
    mysql_close($this->dbConnx);
       }
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    So it's not necessarily considered needed practice to unset all of your variables? Just things like closing MySQL connections?

  6. #6
    Join Date
    Jun 2003
    Location
    here
    Posts
    4,551
    Normal PHP cleanup will occur, both after the destructor and at the end of the page(Triggering the destructor if required).

    What this means is that you don't need to free every variable, just do things PHP won't do normally.
    If you are using PHP please use the [PHP] and [/PHP] forum tags for highlighting...
    The same applies to HTML and the forums [HTML][/HTML] tags.

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Variables within an object will be destroyed along with the object automatically. Something like a resource from a mysql_connect() is not, however, even though the variable is destroyed, as that variable is just storing the resource ID, not the resource itself.

    Frankly, I've never run into a situation yet where I needed to use a destructor. That's not to say there might not have been a few occasions where my code might have been slightly improved by doing so (maybe a bit less resource-intensive) but nothing where it would have mattered functionally -- or at least that I was aware of.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    I've used a deconstructor to finish writing a file and close the resource handler. Looking back I should've closed the resource handler immediately following the file access/write since the active portions that use that code essentially enforce a single user at a time to have access.

  9. #9
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    Is a destructor called automatically or do you have to call it when you're done like:
    PHP Code:
    $a->__destruct(); 

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    From the manual:

    "The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence....The destructor will be called even if script execution is stopped using exit(). Calling exit() in a destructor will prevent the remaining shutdown routines from executing."
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    Does that include die()? I know that die() and exit() do the same thing, but I wasn't sure if equivalent functions/language constructs are included when their siblings are.

  12. #12
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    Since the manual says that die() is "equivalent to exit()", I would assume so. But if you don't like assuming such things, all I could suggest would be to write a test script to find out.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  13. #13
    Join Date
    Dec 2007
    Location
    Mississippi
    Posts
    1,063
    As always, you three have helped me out. Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles