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
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-16-2009, 12:48 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default Destroying a class object

Quick question about best practice:

When i create an object i use the standard contructor method. Is there an equivalent for destroying an object? If so what is it? If not, what is best practice - should i be destroying objects when i have finished with them?

Cheers,

Capt Merton
captainmerton is offline  
Reply With Quote
Old 05-16-2009, 01:19 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by captainmerton View Post
Quick question about best practice:

When i create an object i use the standard contructor method. Is there an equivalent for destroying an object? If so what is it? If not, what is best practice - should i be destroying objects when i have finished with them?

Cheers,

Capt Merton
You can use the __destruct() construct. The only difference with having one in your class and not having one is that if you don't have one, PHP will destroy your object at the end of the executed script.

As long as there are no more references to the object it will get destroyed.
allworknoplay is offline  
Reply With Quote
Old 05-16-2009, 01:38 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Just use unset() as you would any other variable.
Salathe is offline  
Reply With Quote
Old 05-16-2009, 02:34 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

You generally don't have to worry about memory management in PHP, scripts just don't run long enough for leaks to be a problem. This is not always the case in extremely complicated tasks though,
__________________

Village Idiot is offline  
Reply With Quote
Old 05-16-2009, 07:08 PM   #5 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

I'm trying to use it for a logout method in a login class. So when someone is logged in (stored in session) and want to logout i want to kill the session and the class. having problems clearing out the session though.
captainmerton is offline  
Reply With Quote
Old 05-16-2009, 08:01 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How about clearing the session on the __destruct?

php Code:
class TalkPHP_Login
{
    public function __destruct()
    {
        session_destroy();
        unset($_SESSION);
    }
}

$pLogin = new TalkPHP_Login();
unset($pLogin);
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-16-2009, 08:21 PM   #7 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
How about clearing the session on the __destruct?

php Code:
class TalkPHP_Login
{
    public function __destruct()
    {
        session_destroy();
        unset($_SESSION);
    }
}

$pLogin = new TalkPHP_Login();
unset($pLogin);
What's the significance of calling session_destroy() before using unset() ??
allworknoplay is offline  
Reply With Quote
Old 05-16-2009, 11:01 PM   #8 (permalink)
The Wanderer
 
gregor171's Avatar
 
Join Date: May 2009
Location: Ljubljana, Slovenia
Posts: 9
Thanks: 0
gregor171 is on a distinguished road
Default

I came into PHP from other "more" OOP languages and a practice there is to destroy object by:
$my_object = null;
It's a good habit.

The usage of destructor is to clean object before it's destroyed. That would be to close database connections (that could stay open and this is a bad thing to happen), write some logs etc.

Quote:
Originally Posted by Wildhoney View Post
public function __destruct()
{
session_destroy();
unset($_SESSION);
}

[/highlight]
this won't quite work! variables must be accessible and unset does nothing here.
PHP Code:
public function __destruct(){
             global 
$_SESSION;
        
session_destroy();
        unset(
$_SESSION);

Generally clearing the session in destructor seems a good idea, but be careful that this is done on logout only.
__________________
WEB Developer
http://xweblabs.com
http://grajzar.info
Send a message via Skype™ to gregor171
gregor171 is offline  
Reply With Quote
Old 05-16-2009, 11:17 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by gregor171 View Post
PHP Code:
public function __destruct(){
             global 
$_SESSION;
        
session_destroy();
        unset(
$_SESSION);

Generally clearing the session in destructor seems a good idea, but be careful that this is done on logout only.
Interesting, but why do you have to define the $_SESSION globally when it's already a super global array?
allworknoplay is offline  
Reply With Quote
Old 05-16-2009, 11:54 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
How about clearing the session on the __destruct?

php Code:
class TalkPHP_Login
{
    public function __destruct()
    {
        session_destroy();
        unset($_SESSION);
    }
}

$pLogin = new TalkPHP_Login();
unset($pLogin);
Why would anyone want to destroy the session at the end of every request (which uses that class)?

Quote:
Originally Posted by allworknoplay View Post
What's the significance of calling session_destroy() before using unset() ??
They both do different things. session_destroy destroys data associated with the session, which by default deletes the session file. unset simply unsets the $_SESSION variable.

Quote:
Originally Posted by gregor171 View Post
variables must be accessible and unset does nothing here.
PHP Code:
public function __destruct(){
             global 
$_SESSION;
        
session_destroy();
        unset(
$_SESSION);

The $_SESSION is what's termed "superglobal" in PHP meaning that it is available in any scope. As such, there is no need to use the global keyword as above.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
allworknoplay (05-17-2009)
Old 05-16-2009, 11:46 PM   #11 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It all depends how you're using your classes, of course, because __destruct will also be called when the script is ended naturally.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-17-2009, 12:02 AM   #12 (permalink)
The Wanderer
 
gregor171's Avatar
 
Join Date: May 2009
Location: Ljubljana, Slovenia
Posts: 9
Thanks: 0
gregor171 is on a distinguished road
Default

PHP Code:
Interestingbut why do you have to define the $_SESSION globally when it's already a super global array? 
Sorry! It's a habit
__________________
WEB Developer
http://xweblabs.com
http://grajzar.info
Send a message via Skype™ to gregor171
gregor171 is offline  
Reply With Quote
Old 05-17-2009, 08:52 AM   #13 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

Thanks for all your comments guys.
captainmerton 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design