common practice?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

common practice?

Post by s.dot »

I know it's common practice to free mysql result sets. But is it common to unset() all variables and arrays before the script finishes?

I've been doing this a lot lately. Is there any case where this could hurt the execution of the script?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not aware of it having ill effects on the script per se. I have had a few situations where objects weren't being deleted properly (all children being deleted first) but that happened a bit random (switching machines would change behaviour too, truely not good). Switching to 5.1 fixed it so far though.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

so is it good practice? especcially with scripts that consume a lot of memory
is it common practice?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's good practice to dump variables when you no longer need them.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Interesting. I haven't done it myself, so far. Probably because in most (all?) tutorials, articles and books I've read it isn't done. But I can see the "good practice" issue about it. Especially when apps get more complicated with multiple includes etc.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Re: common practice?

Post by Ree »

scottayy wrote:But is it common to unset() all variables and arrays before the script finishes?
I really don't see the point to do this right before the end of the script.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

i didn't mean right before the end of the script

im talking like i get obsessive with it lately =/

like this

Code: Select all

$array = array();
for($i=0;$i<$whatever;$i++){
   $array[] = $i
}

unset($i);
unset($whatever);

//    do stuff with $array
unset($array);

//    do more code
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

In this case it makes sense, yes. However, I find myself doing it very rarely, usually in cases when I manipulate huge collections. I really never care about unsetting a month array or anything of this 'caliber'. ;)
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Code: Select all

$array = array();
for($i=0;$i<$whatever;$i++){
   $array[] = $i
}

unset($i, $whatever); // one call

//    do stuff with $array
unset($array);

//    do more code
I really don't see the point to do this right before the end of the script.
I've seen code from Ilia Alshanetsky where he unset() just before an exit which makes me wonder.

Code: Select all

//From: Managing PHP Performance By Ilia Alshanetsky
function cache_start() 
{ 
    global $cache_file_name; 
 
    // a superbly creative way for creating cache files 
    $cache_file_name = __FILE__ . '_cache'; 
 
    $age = 600; // default cache age 
     
    // check if cache exists and is valid 
    if (@filemtime($cache_file_name) + $age > time()) { 
        // Yey! cache hit, output cached data and exit 
        readfile($cache_file_name); 
        unset($cache_file_name); exit; 
    } 
    ob_start(); // nothing in cache or cache is too old 
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It never hurts to help php clear memory. What if the version you are working on has some memory leaks? If memory serves, a while ago if you didn't clear certain things it would leak in GD or some other commonly used extension.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: common practice?

Post by alex.barylski »

scottayy wrote:I know it's common practice to free mysql result sets. But is it common to unset() all variables and arrays before the script finishes?

I've been doing this a lot lately. Is there any case where this could hurt the execution of the script?
It's not nessecary at all...

http://ca.php.net/manual/en/language.ty ... f-destruct

GC takes care of that for you ;)

I think the primary reason for PHP even having those functions are for occassions when you need to explicitly free a resource (mysql connection, file handles, etc) so you can create a new one and assign it to the same variable.

Code: Select all

$fp = fopen('somefile.txt', 'r');
$buff = fread($fp, filesize('somefile.txt'));
fclose($fp);

$fp = fopen('somefile2.txt', 'r');
$buff2 = fread($fp, filesize('somefile2.txt'));
fclose($fp);
PHP functions like mysql_* are typically just wrappers around the original c/c++ API and those languages don't have GC so explicit freeing of resources is mandatory.

Explicit freeing of resources may also be required in PHP if you were using GTK (or similiar) to develop desktop applications, etc...where scripts could run for weeks at at a time without termination.

So, IMHO your being redundant... :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I agree with Hockey here. From what I've read, including in the Zend documentation for writing extensions, the ZE has some advanced memory management features. Then again, what feyd mentioned about memory leaks in <place-name-of-some-big-library-here> rings a bell.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

scottayy wrote:Due to the reference-counting system introduced with PHP 4's Zend Engine, it is automatically detected when a resource is no longer referred to (just like Java). When this is the case, all resources that were in use for this resource are made free by the garbage collector.
If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

scottayy wrote:

Code: Select all

Due to the reference-counting system introduced with PHP 4's Zend Engine, it is automatically detected when a resource is no longer referred to (just like Java). When this is the case, all resources that were in use for this resource are made free by the garbage collector.
If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)
I'm not sure what my local PHP (version 3.2 something) config is, whether I have STRICT set or whatver it's called, but I just called unset() twice sequentially and didn't get any error. I called unset() on a variable which wasn't declared or defined yet.

Can you give me the error message? I'm curious what it says...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

scottayy wrote:
scottayy wrote:Due to the reference-counting system introduced with PHP 4's Zend Engine, it is automatically detected when a resource is no longer referred to (just like Java). When this is the case, all resources that were in use for this resource are made free by the garbage collector.
If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)
I think that basically mean there will still be a pointer to the data if it could still be accessed.

For example, when you're inside a function every variable you create will be available only in the local scope of the function, therefore, when the function ends, the memory used to hold those variables will be freed automatically. If you have a global variable defined in your script I don't think ZE will delete it from memory until the script ends.

In other languages (C/C++...) if you create a pointer to something in memory and then you leave the scope of that pointer without freeing the memory up with "delete" you've just lost X Blocks of memory that you can never recover. PHP avoids that with ZE.

Hmm... I guess the use of unset() could be valuable in large scripts where global data is only needed for a short section of the script.
Post Reply