在 PHP 中,将变量声明为全局函数内部变量或将变量作为参数传递给函数有什么区别?
In PHP, what is the difference between declaring a variable as global inside function, or passing the variable as an argument to the function?
在函数中将变量声明为 global
或 public/private
VS 将其作为参数传递给函数有什么区别?
其他相关的困惑
我最近让自己很头疼,试图将数组变量作为 global
传递给函数并在内部编辑它并希望 return 它改变了,我花了几个小时才弄清楚我需要通过引用将它作为参数传递给函数,比如 functionCall(&$arrayVar);
第二个问题:但我仍然想知道,为什么将变量作为 global
传递然后编辑它并用return
或者简单地通过连接变量数组之类的操作?
我最近 运行 的另一个例子是为 PHPMailer 创建一个函数,我向它传递了几个参数,比如电子邮件收件人地址和消息正文,但我还需要向它传递身份验证字符串,比如API key等等。在这里,我每次都调用它:
- 我不想在每次调用 PHMailer 函数时都向它传递身份验证凭据(例如,在几个阶段之一通过电子邮件发送错误消息)
- 但我确实想在每次调用它时都传递唯一的参数
所以我认为最好的方法是这样的:
function phpMailer( $mail_to = "to@email.com", $mail_from = "a@b.com" ) {
global $authCredentials;
}
// And of course, when I call phpMailer, I call it like
phpMailer("that.guy@there.com", "me@here.com");
第三个问题: global
的这种用法是否合适,或者我应该采用其他方式吗?
这里有很多问题,我会尝试解决它们...
What is the difference between declaring a variable within a function as global or public/private VS passing it to a function as an argument?
global
是 variable scope 的一种。将变量声明为 global
通常被认为是不好的做法,您应该尽量避免这种做法。通过将变量作为参数传递给函数,代码的可重用性更高,因为您知道函数期望什么并且它不依赖于一些未知的神秘全局变量。
public
、private
、protected
是visibility used in object oriented programming的类型。这些基本上决定了 class 中的属性和方法如何被其他 class 访问。
it took me hours to figure out that I needed to pass it into the function as an argument by reference
关于函数需要了解的一点是,除非您 pass arguments by reference 您正在使用变量的副本,而不是原始变量。
why doesn't it work to pass the variable in as global
then edit it and spit it back out with return
or simply by doing something like concatenating to the variable array?
您不需要 return
一个 global
变量,因为您使用的是原始值。请再次参考上面关于范围的link。
Another example I recently ran into is by making a function for PHPMailer, where I am passing it several arguments, like email-to address and message body, but I also need to pass it authentication strings, like API key, etc.
除了使用 global
之外,还有几种方法可以解决这个问题。如果您计划在多个地方使用此身份验证密钥,最简单的解决方案可能是定义一个 constant,例如:
define('AUTH', 'my_key');
function phpMailer( $mail_to = "to@email.com", $mail_from = "a@b.com" ) {
echo AUTH;
}
但同样,该函数现在的可重用性较低,因为它依赖于该常量。更好的解决方案可能是将其包装在一个对象中:
class phpMailer()
{
private $auth = 'my_key';
public function send($mail_to, $mail_from)
{
$this->auth;
}
}
$mail = new phpMailer();
$mail->send('to@email.com', 'a@b.com');
希望这对您有所帮助。在上述 link 中找到的在线 PHP 文档包含大量信息。
在函数中将变量声明为 global
或 public/private
VS 将其作为参数传递给函数有什么区别?
其他相关的困惑
我最近让自己很头疼,试图将数组变量作为 global
传递给函数并在内部编辑它并希望 return 它改变了,我花了几个小时才弄清楚我需要通过引用将它作为参数传递给函数,比如 functionCall(&$arrayVar);
第二个问题:但我仍然想知道,为什么将变量作为 global
传递然后编辑它并用return
或者简单地通过连接变量数组之类的操作?
我最近 运行 的另一个例子是为 PHPMailer 创建一个函数,我向它传递了几个参数,比如电子邮件收件人地址和消息正文,但我还需要向它传递身份验证字符串,比如API key等等。在这里,我每次都调用它:
- 我不想在每次调用 PHMailer 函数时都向它传递身份验证凭据(例如,在几个阶段之一通过电子邮件发送错误消息)
- 但我确实想在每次调用它时都传递唯一的参数
所以我认为最好的方法是这样的:
function phpMailer( $mail_to = "to@email.com", $mail_from = "a@b.com" ) {
global $authCredentials;
}
// And of course, when I call phpMailer, I call it like
phpMailer("that.guy@there.com", "me@here.com");
第三个问题: global
的这种用法是否合适,或者我应该采用其他方式吗?
这里有很多问题,我会尝试解决它们...
What is the difference between declaring a variable within a function as global or public/private VS passing it to a function as an argument?
global
是 variable scope 的一种。将变量声明为 global
通常被认为是不好的做法,您应该尽量避免这种做法。通过将变量作为参数传递给函数,代码的可重用性更高,因为您知道函数期望什么并且它不依赖于一些未知的神秘全局变量。
public
、private
、protected
是visibility used in object oriented programming的类型。这些基本上决定了 class 中的属性和方法如何被其他 class 访问。
it took me hours to figure out that I needed to pass it into the function as an argument by reference
关于函数需要了解的一点是,除非您 pass arguments by reference 您正在使用变量的副本,而不是原始变量。
why doesn't it work to pass the variable in as
global
then edit it and spit it back out withreturn
or simply by doing something like concatenating to the variable array?
您不需要 return
一个 global
变量,因为您使用的是原始值。请再次参考上面关于范围的link。
Another example I recently ran into is by making a function for PHPMailer, where I am passing it several arguments, like email-to address and message body, but I also need to pass it authentication strings, like API key, etc.
除了使用 global
之外,还有几种方法可以解决这个问题。如果您计划在多个地方使用此身份验证密钥,最简单的解决方案可能是定义一个 constant,例如:
define('AUTH', 'my_key');
function phpMailer( $mail_to = "to@email.com", $mail_from = "a@b.com" ) {
echo AUTH;
}
但同样,该函数现在的可重用性较低,因为它依赖于该常量。更好的解决方案可能是将其包装在一个对象中:
class phpMailer()
{
private $auth = 'my_key';
public function send($mail_to, $mail_from)
{
$this->auth;
}
}
$mail = new phpMailer();
$mail->send('to@email.com', 'a@b.com');
希望这对您有所帮助。在上述 link 中找到的在线 PHP 文档包含大量信息。