在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?

查看:146
本文介绍了在PHP中,将变量声明为全局内部函数或将变量作为参数传递给函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在函数中声明一个变量为 global public / private 将VS传递给一个函数作为参数?



其他相关混淆

导致我非常头痛,试图将一个数组变量传递给一个函数,如 global ,然后在里面进行编辑并希望将其返回,并花了我数小时才弄清楚我需要通过引用将它作为参数传递给函数,如 functionCall(& $ arrayVar);



次要问题:但我仍然想知道,为什么它不能以 global 的形式传递变量,然后编辑它并将其吐出与 return >或者简单地通过像串联到变量数组一样

我最近碰到的另一个例子是为PHPMailer创建一个函数,其中我传递了几个参数,如电子邮件 - 地址和消息正文,但我也需要传递它的认证字符串,如API密钥等。在这里,每次我打电话给它:


  1. 我不想在每次调用PHMailer函数时都传递身份验证凭证(例如,通过几个阶段之一发送错误消息)。
  2. 但我确实每次我打电话给它时都要传递唯一的参数



  3. 所以我想最好的办法就是这样:

     函数phpMailer($ mail_to =to@email.com,$ mail_from =a@b.com){
    global $ authCredentials;
    }

    //当然,当我调用phpMailer时,我把它称为
    phpMailer(that.guy@there.com,me@here.com );

    第三个问题: 全球还是有一些其他的方式,我应该这样做吗?

    解决方案

    有很多问题在这里,我会尝试通过它们......


    在函数中声明一个变量是全局的还是公共的/ private VS将它传递给一个函数作为参数?


    global 是一种变量范围。将变量声明为 global 通常被认为是不好的做法,您应该尽量避免它。通过将变量作为参数传递给函数,代码更具可重用性,因为您知道函数期望的内容,并且不依赖于某个未知的神秘全局变量。



    public private protected visibility 用于面向对象编程。这些基本上决定了一个类中的属性和方法如何被其他类访问。​​


    花了我几小时才弄清楚我需要通过引用将其作为参数传递给函数


    有关函数的一些理解是,除非通过引用传递参数您正在使用变量的副本,而不是原始的。


    为什么它不能将变量作为 global 传递编辑它,然后用 return 将它吐出来,或者简单地通过像连接到变量数组之类的东西来完成它?

    您不需要 return a 全局变量,因为您正在使用原始值。


    我最近遇到的另一个例子是为PHPMailer创建一个函数,它有几个参数,比如电子邮件地址和邮件正文,但是我还需要传递验证字符串,比如API密钥等。



    <除了使用 global 之外,还有几种方法可以解决这个问题。如果您打算在多个地方使用此身份验证密钥,最简单的解决方案可能是定义

      define('AUTH','my_key'); 
    函数phpMailer($ mail_to =to@email.com,$ mail_from =a@b.com){
    echo AUTH;
    }

    但是,函数现在不太可重用,因为它依赖于该常量。更好的解决方案可能是将它包装在一个对象中:

      class phpMailer()
    {
    私人$ auth ='my_key';

    公共函数发送($ mail_to,$ mail_from)
    {
    $ this-> auth;
    }
    }
    $ mail = new phpMailer();
    $ mail-> send('to@email.com','a@b.com');

    希望这会有所帮助。上面链接中的在线PHP文档包含丰富的信息。


    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?

    Other related confusion

    I've recently caused myself a big headache trying to pass aa array variable into a function as global and editing it inside and hoping to return it altered, and it took me hours to figure out that I needed to pass it into the function as an argument by reference, like functionCall(&$arrayVar);

    Secondary question: But I am still wondering, 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?

    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. Here, each time I call it:

    1. I don't want to have to pass it the authentication credentials every time I call the PHMailer function (eg, to email error message at one of several stages)
    2. But I do want to pass it unique arguments every time I call it

    So I figured the best way is like this:

    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");
    

    Tertiary question: Is this appropriate usage of global or is there some other way I should be doing this?

    解决方案

    There are a lot of questions here, I will try to walk through them...

    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 is a type of variable scope. Declaring a variable as global is generally considered bad practice and you should try to avoid it. By passing the variable to a function as an argument, the code is more reusable because you know what the function expects and it isn't relying on some unknown mystery global variable.

    public, private, protected are types of visibility used in object oriented programming. These basically determine how properties and methods within a class can be accessed by other classes.

    it took me hours to figure out that I needed to pass it into the function as an argument by reference

    Something to understand about functions is that unless you pass arguments by reference you are working with a copy of the variable, not the original.

    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?

    You wouldn't need to return a global variable, because you're working with the original value. Please refer again to the link above about scope.

    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.

    There are several ways to approach this in addition to using global. If you are planning to use this authentication key in more than one place, the easiest solution would probably be to define a constant, for example:

    define('AUTH', 'my_key');
    function phpMailer( $mail_to = "to@email.com", $mail_from = "a@b.com" ) {
        echo AUTH;
    }
    

    But again, the function is now less reusable because it is dependent on that constant. A better solution would probably be to wrap it in an object:

    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');
    

    Hopefully this helps. The online PHP documentation found in the links above contain a wealth of information.

    这篇关于在PHP中,将变量声明为全局内部函数或将变量作为参数传递给函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆