开发者

Set local variable to null in PHP

开发者 https://www.devze.com 2023-04-03 10:18 出处:网络
Very often in code added by my more .NET oriented colleagues开发者_如何学Python, I\'ll run into something like this:

Very often in code added by my more .NET oriented colleagues开发者_如何学Python, I'll run into something like this:

function someFunction()
{
    $localVariable = otherFunction();
    $ret = $localVariable * 2; // or whatever

    $localVariable = null;
    return $ret;
}

Is there any benefit to setting $localVariable to null? Since it's a local variable (and therefore will run out of scope anyway), I would assume not, but please do correct me if I'm wrong.


Your assumption would be correct for both php and .NET.


There are two things to consider here: memory management and defensive coding.

For memory management purposes, setting things to null is pretty much pointless. The garbage collector can figure things out just fine, and everything will be fine once the function exits. For smaller pieces of memory (like an integer) this just clutters up the code.

HOWEVER, for defensive coding purposes, this might be ok. This example is just silly, but sometimes in your code it's a good idea to set things to null when you're done using them, so that if they DO get used later on in the function, it blows up in the programmers face (and is immediately obvious) rather than leading to bugs that are hard to track down.


No, it could be useful only if there's more heavy processing after it in order to free resources. But unset() would be more useful in that case anyway.


As CG says, you are correct in assuming this is unnecessary.

However, it MAY be more relevant if more memory intensive code comes afterward, like in this function:

function someFunction()
{
    $localVariable = new MemoryPiggy();
    $number = $localVariable->calcValue();

    $localVariable = null;

    $localVariable2 = new AnotherMemoryHog($number);
    /*
     * Do stuff with $localVariable2
     */

    return $ret;
}

I think this is why most people would prematurely force a variable out of scope: to allow the garbage collector to reclaim that space if it wants to, knowing that you won't be using it.

P.S. This is an example where the next part of the code also gobbles up memory. Another example might be where the 2nd part of the code takes a long, long time to execute. Barring situations like these, though, there is no need to increase the complexity of your program by nulling out locals.


No.

Trust PHP to manage this stuff for you.


No.

 

If you need too free variable's memory, you'd better use unset().

However, it may be helpful if $localVariable is a big object or array. Making it null will turn data unlinked, thus making it next to be deleted.

Although you don't need to do any of that, because all local variables will be freed after function has ended.


The only situation in which that would make sense is if you had someone who learned to code in VBScript. In VBScript, there was a lot of magic around reference counting and cleaning up unused memory, so many people got in the habit of setting things to null (or nothing in vbspeak). If your .Net colleagues have been around since the days of ASP (not ASP.Net) it's somewhat likely that they are still in the habit of doing this. Although the other posters are right, in PHP and in .Net (VB or C#) it does nothing of any significance.

0

精彩评论

暂无评论...
验证码 换一张
取 消