TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-15-2008, 11:34 AM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2008
Location: Cloud 9
Posts: 15
Thanks: 0
Jenski is on a distinguished road
Help When is __destruct() called?

Hi,
I'm I've been programming in PHP4 for a few years now and am slowly moving over to PHP5, I was wondering when developing a class when the __destruct() function is called? I have put all my closing connections in it, and tried to print to screen for confirmation but it doesn't print anything.

Any thoughts?

Thanks in advance
Jenski is offline  
Reply With Quote
Old 07-15-2008, 03:31 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default

It is called when you destroy the object. So, for example:

php Code:
class Destruct_Example
{
    public function __construct()
    {
        echo "Called Construct()";
    }
   
    public function __destruct()
    {
        echo "Called Destruct()";
    }
}

$pExample = new Destruct_Example();
unset($pExample);

And it will also be called at the end of the script.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
The Following User Says Thank You to Karl For This Useful Post:
Geert (07-16-2008)
Old 07-15-2008, 03:51 PM   #3 (permalink)
The Contributor
 
redSHIFT's Avatar
 
Join Date: Jan 2008
Location: England, UK
Posts: 69
Thanks: 3
redSHIFT is on a distinguished road
Default

It's quite useful in database abstraction classes, for example:

PHP Code:
abstract class Database
{
    public function 
__construct()
    {
        
$this->connect();
    }
    
    abstract public function 
connect();
    
/*...*/
    
    
public function __destruct()
    {
        
$this->conn->close();
    }

(although on second thoughts closing it on script exit isn't a huge memory saving :P).
redSHIFT is offline  
Reply With Quote
Old 07-15-2008, 07:26 PM   #4 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 359
Thanks: 3
xenon is on a distinguished road
Default

The destructor is always called when the object is destroyed, set to null, or to another data type. If you don't do any of the above specified operations, the destructor will be automatically called after the script has ended (finished processing).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 08-04-2008, 10:16 AM   #5 (permalink)
The Wanderer
 
Join Date: Apr 2008
Location: Cloud 9
Posts: 15
Thanks: 0
Jenski is on a distinguished road
Default

Is it always called when the script has ended though?

Any reason why it doesn't?
Jenski is offline  
Reply With Quote
Old 08-04-2008, 07:17 PM   #6 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 359
Thanks: 3
xenon is on a distinguished road
Default

Well, technically, no. The destructor is called when the script has ended, but the memory allocated for the object remains allocated until the garbage collector comes and wipes everything related to the php that's not used anymore. If you were asking if objects are persistent from one page to another, then no. Nothing in PHP is persistent, as all cgi modules use the HTTP protocol as a foundation to run on. And HTTP is a stateless protocol, as we all have learned.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:55 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0