CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question Constructor and Desctructors in PHP? Do I need to delete/clean stuff? [PHP5]

    I've created some PHP classes that, in the constructor, create an instance (object) of another PHP class (for example a database), does this need to be cleaned up somehow? For example is there such a thing as a destructor [function() ~function()] in PHP5 and is it needed?

    For example - check out the following code I have for a class called Tools.class.php (for example)
    Code:
    {
        private $db = NULL;             // Database Interface
    
        ////////////////////////////////////////////////////////////////////////////
        // Constructor
        function Tools()
        {
            $this->db = new Database();
        }
    
        function DoStuff()
        {
        ... do stuff ...
        }
    So, in the constructor of tools I create an instance of Datbase ($db), and then in DoStuff() I use that connection to get/set data from the Database ... All this is great but what happens when Tools is no longer of any use? Meaning I have a PHP file that does:
    Code:
    $tool = new tools();
    $tool->DoStuff();
    But that is it ... Do I need to delete $tool somehow?
    And within Tools.class.php do I need to somehow cleanup $db?

    I don't want to cause any memory leak or Database connection issues.

    Any help would be much apprecited.
    Thanks,

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Constructor and Desctructors in PHP? Do I need to delete/clean stuff? [PHP5]

    Rather than re-write the whole article for you...take a look at this.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured