引用函数作用域之外的变量的更好方法是什么?

发布于 2024-12-04 05:28:22 字数 260 浏览 1 评论 0原文

我可以通过以下两种方式之一更改函数中的 $var:通过引用传递或使用 global 关键字。

$var1 = 10;

function test1() {
    global $var1;
    $var1++;
}

function test2(&$var) {
    $var++;
}

两种方法都有相同的结果,但是它们之间有什么区别吗?哪一种是首选,哪一种更快?

I can change $var in my function in one of two ways: either pass it by reference or using the global keyword.

$var1 = 10;

function test1() {
    global $var1;
    $var1++;
}

function test2(&$var) {
    $var++;
}

Both approaches have the same result, but is there any difference between them? Which one is preferred and which one is faster?

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

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

发布评论

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

评论(3

美羊羊 2024-12-11 05:28:22

1.它们都不是首选。

除非您有特殊原因要这样做,否则首选是

$var1 = 10;
$var1 = test3($var1);
function test3($var)
{
    return $var + 1;
}

在程序的不同部分之间引入耦合(如果使用全局)是您应该始终做的事情合理地尽量避免。

此外,如果没有具体原因让您的函数通过引用接受其参数,您也应该避免这样做。所有函数中只有极小一部分会以这种方式运行,因此如果不出意外的话,您可能会冒着让使用此代码而没有真正好处的开发人员感到困惑的风险。

2.您不需要考虑哪个更快。

除非您在现实场景下分析了您的应用程序并发现该函数是一个瓶颈(当然它永远不会以这种简单的形式出现),那么以牺牲编写清晰且可维护的代码为代价来优化性能不仅毫无意义,而且是有害的。

作为奖励,我应该提到,使用引用实际上可能会使函数 更慢

1. None of them is preferred.

Unless you have a special reason to do otherwise, the preferred would be

$var1 = 10;
$var1 = test3($var1);
function test3($var)
{
    return $var + 1;
}

Introducing coupling between different parts of your program (if using a global) is something you should always reasonably try to avoid.

In addition, if there is no concrete reason to make your function accept its argument by reference you should also avoid doing that. Only a very miniscule fraction of all functions behave this way, so if nothing else you are risking confusion among the developers who use this code for no real benefit.

2. You do not need to think about which one is faster.

Unless you have profiled your application under real world scenarios and have found that this function is a bottleneck (which of course it will never be in this simple form), then optimizing for performance at the expense of writing clear and maintainable code is not only pointless, but also detrimental.

As a bonus, I should mention that using a reference might actually make the function slower.

盗梦空间 2024-12-11 05:28:22

由于全局变量会污染命名空间(即可能会无意中使用和/或被具有相同想法的另一个函数使用),因此最好使用引用。

但是,在许多情况下(数据结构更复杂),您应该使用 相反,像这样:

class Counter {
  private $val = 10;
  public function increment() {
    $this->val++;
  }
}

这些解决方案的速度并不重要,并且与任何实际计算相比都相形见绌。

Since global variables pollute the namespace (i.e. can be used inadvertently and/or by another function with the same idea), references are preferable.

However, in many cases (where the data structures are more complex), you should be using objects instead, like this:

class Counter {
  private $val = 10;
  public function increment() {
    $this->val++;
  }
}

The speed of any of these solutions does not matter and will be dwarfed by any actual computation.

远昼 2024-12-11 05:28:22

首选方法是避免全局变量。原因是,如果你将一个变量放在全局范围内,你就会失去对它的控制——随着项目的增长,你可能会忘记全局变量的名称是什么,并且你可能会在某个地方意外地覆盖它,这会给你自己带来令人难以置信的头痛。

从性能的角度来看 - 引用速度更快,而且使用起来也更安全,因为您可以在方法的签名中定义是否使用引用,从而使实际的函数调用变得容易,因为您不必显式地通过引用传递变量。

Preferred way is avoiding globals. The reason is that if you put a variable in global scope, you lose control over it - since projects grow, you might forget what the name of your global variable is and you can overwrite it accidentally somewhere causing an incredible headache for yourself.

From performance point of view - reference is faster and it's also much safer to use because you define in method's signature whether a reference is being used or not, making the actual function call easy as you don't have to pass the variable by reference explicitly.

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