访问嵌套函数内的全局变量

发布于 2024-11-07 07:16:22 字数 667 浏览 0 评论 0原文

这是我的场景:

$foo = bar;

one()
{
   two()
   {
      three()
      {
         // need access to $foo here
      }
   }
}

我知道我可以将 $foo 传递给所有三个函数,但这并不是很理想,因为您可能需要也可能不需要它,此外,已经有其他参数,我不想添加它列出我是否可以阻止它。只有当您到达 two() 时才需要 $foo。我知道我也可以在 two() 中指定 global $foo;,但我读到这不是一个好的做法。

是否有另一种方法可以使 $foo 可供 two() 使用,而不需要执行其中任何一个?

为了提供更多信息,本例中的 $foo 是有关如何连接到数据库的信息,例如服务器、用户、通行证,但只有在 Three() 中它才真正需要该信息。当然,我可以在文档加载时立即连接到数据库,但如果文档可能不需要它,那就没有必要了。

有什么想法吗?

更新:抱歉,我的意思并不是在每个函数的开头都有“函数”,这意味着我是以嵌套方式创建它们的。

Here's my scenario:

$foo = bar;

one()
{
   two()
   {
      three()
      {
         // need access to $foo here
      }
   }
}

I know I could pass $foo down all three functions, but that isn't really ideal, because you may or may not require it, besides, there are other parameters already and I don't want to add to that list if I can prevent it. It's only when you get to three() do you need $foo. I know I could also specify global $foo; in three(), but I read that this wasn't a good practice.

Is there another way $foo can be made available to three() without doing either of those?

To provide a little more info, $foo in this case is info on how to connect to the database e.g. server, user, pass, but only in three() does it actually need that info. Of course I could connect to the database right as the document loads, but that feels unecessary if the document might not need it.

Any ideas?

UPDATE: I'm sorry, I didn't mean to have "function" at the beginning of each function implying that I was created them in a nested way.

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

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

发布评论

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

评论(3

独守阴晴ぅ圆缺 2024-11-14 07:16:22

是的,您可以使用global,或者将其作为参数传递。两者都是公平的方式。然而,我会选择后者。或者,您也可以使用 闭包use关键字:

$foo = 'bar';

$one = function() use($foo) {
   $two = function() use($foo) {
      $three = function () use($foo) {
         // access to $foo here
      };
   };
};

如果您需要修改 $third()$foo 的值,也可以是 use(&$foo) >

Yes, you can use global, or pass it through as a parameter. Both are fair ways. However, I'd do the latter. Or, you can also use closures and the use keyword:

$foo = 'bar';

$one = function() use($foo) {
   $two = function() use($foo) {
      $three = function () use($foo) {
         // access to $foo here
      };
   };
};

Can also be use(&$foo) if you need to modify the top $foo's value inside of $three()

温柔戏命师 2024-11-14 07:16:22

如果使用得当,使用global并不是一个坏习惯。在这种情况下,您可以使用global,这样就完全没问题了。

由于global使函数可以访问全局变量,因此存在数据损坏的风险。
只需确保谨慎使用,并且您知道自己在做什么(即不要意外更改您不希望更改的数据。这有时会导致一些难以跟踪的令人讨厌的错误。)

总结:global 在这种情况下完全没问题。

真正糟糕的是嵌套函数,至少在 PHP 中是这样。

Using global is not a bad practice if used properly. This case you would use global and it will be totally fine.

Since global makes functions get access to global variables, there are data corruption risks.
Just make sure to use with caution and you know what you're doing (i.e. not accidentally change the data that you don't want it to change. This sometimes causes some nasty bugs that's hard to track.)

Summarize: global in this case is perfectly fine.

What's really bad is nested functions, at least in PHP.

标点 2024-11-14 07:16:22

不要听任何人谈论全球
阅读有关 OOP 的书籍(例如 这本书),阅读有关依赖注入、工厂的内容-模式。
依赖注入是创建灵活且易于维护的代码的最佳方式,根据 DI 哲学,您将在参数中将 $foo 从第一个函数传递到第三个函数,或者将传递带有所有必要变量的 Context 对象(如果您想最小化计数)参数)。
有关 DI 的更多信息,您可以在这里阅读:http://components。 symfony-project.org/dependency-injection/trunk/book/00-简介

Don't listen anybody, who are speaking about global.
Read books about OOP (e.g. this one), read about Dependency Injection, Factory-patterns.
Dependency Injection is the best way to create flexible and easy-to-maintain code, and by DI philosophy you will pass $foo from first to third function in arguments, or will pass Context object with all necessary variables (if you want to minimize count of arguments).
More about DI you can read here: http://components.symfony-project.org/dependency-injection/trunk/book/00-Introduction

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