TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Destroying a class object (http://www.talkphp.com/absolute-beginners/4300-destroying-class-object.html)

captainmerton 05-16-2009 12:48 PM

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

allworknoplay 05-16-2009 01:19 PM

Quote:

Originally Posted by captainmerton (Post 24147)
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.

Salathe 05-16-2009 01:38 PM

Just use unset() as you would any other variable.

Village Idiot 05-16-2009 02:34 PM

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,

captainmerton 05-16-2009 07:08 PM

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.

Wildhoney 05-16-2009 08:01 PM

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);

allworknoplay 05-16-2009 08:21 PM

Quote:

Originally Posted by Wildhoney (Post 24161)
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() ??

gregor171 05-16-2009 11:01 PM

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 (Post 24161)
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.

allworknoplay 05-16-2009 11:17 PM

Quote:

Originally Posted by gregor171 (Post 24174)
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?

Wildhoney 05-16-2009 11:46 PM

It all depends how you're using your classes, of course, because __destruct will also be called when the script is ended naturally.

Salathe 05-16-2009 11:54 PM

Quote:

Originally Posted by Wildhoney (Post 24161)
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 (Post 24162)
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 (Post 24174)
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.

gregor171 05-17-2009 12:02 AM

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 ;-)

captainmerton 05-17-2009 08:52 AM

Thanks for all your comments guys.


All times are GMT. The time now is 12:55 PM.

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