In PHP, what is the difference between declaring a

2019-07-14 12:27发布

问题:

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?

回答1:

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.