Delete a "newed" variable

A

Anonymous

Guest
Hi,

Another question about "new" operator in php.
Everytime if we "new" something, do we need to delete it to free the memory?

I am asking this cause I am using few "new" in a single html page, meaning everytime users refresh, it will call the new operator again, I just want to be sure if I actually need to do some clean up after a new operator.

Thank you very much.
 
everything you create in php script scope will be destroyed when it goes out of <?php ?> (page) scope.
it is good habit to clean yourself ....
 
Are you saying that variables are destroyed when the tags close? Cause this is not the case.

How are you using the new? The new is for objects. As in instantating a clase like...

Code:
class Mega
{
   var $megavar;
   function mega()
   {
       return $this->megavar;
   }
}

$m = new Mega;
echo $m->mega();

I hope you aren't using new for something stupid.
 
:wink:
<?php
$value ="Php-forum";
$m =new Mega();
?>

<?php echo $value ;
$m->showme();
?>

will work
I mentioned .
when it goes out of <?php ?> -->(page) <--- scope
thanx
:idea:
 
Hi,

Thanks for both of your reply.
Yes, I use "new" in the way that Joel just described.

So, Sigix, as long as the code is within a single file, it should work (I assume that the page or <?php ?> that you mentioned is actually refering to a php file), right?.

That means I do not have to worry about leaking any memory.
So, everytime when a php file is being parsed, as soon as the parsing is done, all the memory allocated will be free automatically by php?

Thank you so much.
 
xyoon said:
So, everytime when a php file is being parsed, as soon as the parsing is done, all the memory allocated will be free automatically by php?

Yes, this is correct. Once a PHP script is finished parsing, all of the memory it was using is freed -- all variables are unset and all objects are destroyed. The exception, of course, is session variables, which are stored on the disk (not in memory) until they expire.

However, it can occasionally be useful to use unset() to free memory during the execution of a PHP script. This usually isn't an issue until you have many concurrent connections, but if your script happens to be handling large chunks of data, it's best to get rid of them once you no longer need them. For example, if you're using GD to copy part of an image to another image, once you've copied from the first image, unset it to free up the memory it was using.
 
Back
Top