tags:

views:

4127

answers:

3

I realise the second one avoids the overhead of a function call (update, is actually a language construct), but it would be interesting to know if one is better than the other. I have been using unset() for most of my coding, but I've recently looked through a few respectable classes found off the net that use $var = null instead.

Is there a preferred one, and what is the reasoning?

+3  A: 

By doing an unset() on a variable, you've essentially marked the variable for 'garbage collection' (PHP doesn't really have one, but for example's sake) so the memory isn't immediately available. The variable no longer houses the data, but the stack remains at the larger size. Doing the null method drops the data and shrinks the stack memory almost immediately.

This has been from personal experience and others as well. See the comments of the unset() function here.

I personally use unset() between iterations in a loop so that I don't have to have the delay of the stack being yo-yo'd in size. The data is gone, but the footprint remains. On the next iteration, the memory is already being taken by php and thus, quicker to initialize the next variable.

invenetix
Setting something to NULL can be of benefit if the memory required to hold the value NULL is less than that required to hold whatever value it was previously holding. For example, a long string. If the string wasn't a constant and its reference count drops to zero, then that memory should be freed. Unset is cleaner - it no longer maintains a reference. You do have to wait for garbage collection, but it's safe to treat it as occupying no memory, because a low memory condition will trigger garbage collection.
thomasrutter
+11  A: 

As mentioned in unset

unset() does just what it's name says - unset a variable. It does not force immediate memory freeing. PHP's garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren't needed anyway, or as late as before the script would run out of memory, whatever occurs first.

If you are doing $whatever = null; then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.

Note that until php5.3, if you have two objects in circular reference, such as in a parent-child relationship, calling unset() on the parent object will not free the memory used for the parent reference in the child object. (Nor will the memory be freed when the parent object is garbage-collected.) (bug 33595)

VonC
+5  A: 

unset is not actually a function, but a language construct. It is no more a function call than an echo, a return or an include.

Aside from performance issues, using unset makes your code's intent much clearer.

Alex Barrett
That's why I always used them, personally I thought they looked better than $var = null. By the way, I've always used NULL full caps... but now I don't know why ?
alex
@alex: may be because it is a constant ? See http://drupal.org/node/217379
VonC
@VonC: Yeah, I figured that, but why can you use lowercase true, false and null?
alex
I also made the assumption language constructs didn't use parenthesis, like echo, require, include etc aern't echo(), require() or include().
alex
@alex, you can sort of do that with unset. For example "$test = 4; (unset) $test;" - strange but true, and it returns the value of $test before unsetting it. Regardless, the PHP manual does confirm that it is a language construct.
thomasrutter
@thomas, thanks for the info
alex