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-03-2008, 07:27 PM   #1 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Help unset and __destruct

Hi !

I'm used to unset my local variables but I'm wondering if it is really useful.

For instance, in this case:

PHP Code:
<?php
function toto()
{
  
$myVar=5;
  [...] 
// some treatments
  
unset($myVar);
}
?>
Is the local variable $myVar automatically free when we leave the function toto or is it really useful to unset it ?
Must I just unset the variables instancied by the "new" operator ?

Second question:

I don't know if what I put in my __destruct methods is good.
Indeed, my typical __destruct method is

PHP Code:
public function __destruct()
{
  foreach(
get_object_vars($this) as $k=>$v)
    unset(
$this->$k);

and then, my __destruct() call on an object $foo is automatically followed by unset($foo);

In fact, I don't know the exact behavior of php when it leaves a method and when it has finished to parse my pages.

What do you advise ?

Thank you, have a nice evening !
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
Old 05-03-2008, 07:51 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I may be wrong about this, but I'm pretty sure unset()ing the object will destroy all the variables ($this) contained within that object without the need of explicitly doing so in the __destruct() method.

I also read a note in the manual stating that when if you are planning to destroy an object before the end of the script (ie, half way through, right after using it, etc), that you may want to call __destruct yourself. Otherwise PHP will wait until the end of the script before calling the method and you won't be gaining any resources along the way.
-m
delayedinsanity is offline  
Reply With Quote
Old 05-03-2008, 08:02 PM   #3 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
I may be wrong about this, but I'm pretty sure unset()ing the object will destroy all the variables ($this) contained within that object without the need of explicitly doing so in the __destruct() method.
mmmm, I don't think so.
I think it's the same way as the C or C++ dynamic allocation.

The object is the link between the user and the allocated memory used by dynamic attributes of the class instancied by the object. (Excuse if I'm not clear, I do my best in english ).

In C++, If I have an object foo and an attribute foo->bar which is itself another object of another class, if I do delete(foo) so bar is lost and I will never be able to free it.

I think it's the same in php, even if allocation and types are dynamic but I don't know how/where to verify it properly.
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
Old 05-03-2008, 08:06 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Ah, well you may be right about that. I hardly ever worry about unset()ing much except $_SESSION variables myself. Most of my pages load in less than 0.003 of a second anyways, and that's including my controllers which load multiple objects and do various database calls on top of that. I haven't seen the need, but I'm not suggesting it's not sloppy. I've only been at this for a little while.
-m
delayedinsanity is offline  
Reply With Quote
Old 05-03-2008, 11:50 PM   #5 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

php has a garbage collector, that cleans the variables automatically when they are not referenced or used anymore
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 05-03-2008, 11:54 PM   #6 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

You can use something like this to tset it:

PHP Code:
<?php

class A
{
    public 
$var = array();

    public function 
__construct()
    {
        for (
$i=0;$i<10;$i++) {
            
$this->var[] = new B();
        }
    }
}


class 
B
{
    public 
$varb = array();

    public function 
__construct()
    {
        for (
$i=0;$i<10000;$i++) {
            
$this->var[] = rand();
        }
    }
}


echo 
"Just after load ".intval(memory_get_usage()/(1024*1024))."MB \n";
$a = new A();
echo 
"After creating A() ".intval(memory_get_usage()/(1024*1024))."MB \n";
unset(
$a);
echo 
"After destroying A() ".intval(memory_get_usage()/(1024*1024))."MB \n";
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
The Following User Says Thank You to freenity For This Useful Post:
Gibou (05-04-2008)
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 02:55 PM.

 
     

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