在 PHP 中将局部变量设置为 null

发布于 2024-12-03 12:05:23 字数 321 浏览 2 评论 0原文

在我更面向 .NET 的同事添加的代码中,我经常会遇到这样的情况:

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

    $localVariable = null;
    return $ret;
}

$localVariable 设置为 null 有什么好处吗?由于它是一个局部变量(因此无论如何都会超出范围),我认为不会,但如果我错了,请纠正我。

Very often in code added by my more .NET oriented colleagues, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清秋悲枫 2024-12-10 12:05:23

您的假设对于 php 和 .NET 都是正确的。

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

小霸王臭丫头 2024-12-10 12:05:23

这里需要考虑两件事:内存管理和防御性编码。

出于内存管理的目的,将事物设置为 null 几乎没有意义。垃圾收集器可以很好地解决问题,一旦函数退出,一切都会好起来的。对于较小的内存(例如整数),这只会使代码变得混乱。

然而,出于防御性编码的目的,这可能没问题。这个例子很愚蠢,但有时在你的代码中,当你使用完它们后,将它们设置为 null 是个好主意,这样,如果它们稍后在函数中使用,它就会在程序员面前爆炸(并且是显而易见的)而不是导致难以追踪的错误。

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.

以可爱出名 2024-12-10 12:05:23

不,只有在之后进行更繁重的处理以释放资源时,它才有用。但无论如何,unset() 在这种情况下会更有用。

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.

仙气飘飘 2024-12-10 12:05:23

正如 CG 所说,你认为这是不必要的是正确的。

然而,如果随后出现更多内存密集型代码,那么它可能会更相关,就像在这个函数中一样:

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

    $localVariable = null;

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

    return $ret;
}

我认为这就是为什么大多数人会过早地强制变量超出范围:允许垃圾收集器根据需要回收该空间,并且知道您不会使用它。

PS 这是一个示例,其中代码的下一部分也占用了内存。另一个例子可能是代码的第二部分需要很长很长的时间来执行。不过,除非出现此类情况,否则无需通过消除局部变量来增加程序的复杂性。

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.

幽蝶幻影 2024-12-10 12:05:23

不会。

相信 PHP 会为您管理这些东西。

No.

Trust PHP to manage this stuff for you.

锦爱 2024-12-10 12:05:23

没有

如果你需要太多自由变量的内存,你最好使用unset()

但是,如果 $localVariable 是一个大对象或数组,这可能会有所帮助。将其设置为空将使数据取消链接,从而使其成为下一个被删除的对象。

尽管您不需要执行任何操作,因为所有局部变量都将在函数结束后被释放。

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.

A君 2024-12-10 12:05:23

唯一有意义的情况是您有人学会了用 VBScript 编写代码。在 VBScript 中,引用计数和清理未使用的内存有很多魔力,因此很多人养成了将内容设置为 null(或在 vbspeak 中为空)的习惯。如果您的 .Net 同事从 ASP(不是 ASP.Net)时代就已经存在,那么他们很可能仍然保持这样做的习惯。尽管其他海报是正确的,但在 PHP 和 .Net(VB 或 C#)中,它没有任何意义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文