1

The answers that I have read to this matter have left me more confused than anything. For instance: if the name of the variable is changed, then one has to change every comparison of the variable as global inside functions. But if I pass the variable as a parameter, I still have to change every comparison.

var $myVar = 5;                         //this var was before $myVariable

function myFunc(){
    global $myVariable;                //it needs to be changed
}     

function myFunc($myPar){

}

var $something = myFunc($myVariable);   //it needs to be changed too

2 Answers2

0

Renaming isn't the primary benefit but this is usually easier because your function and the function call will often be in different files. If you have a global that is using a variable set in a different file it can get confusing quickly. If it's used only in a single file a better approach would be to put the function in a class and use a class property to store the variable.

class Example {
    private $variable;

    public function exampleMethod()
    {
        $this->variable; // instead of global
    }
}

As a general rule, try to think of each function/method as an isolated component that takes something (its parameters) and returns something (even if it returns void). What the function does internally doesn't matter and what it gets passed (assuming it's of the right type) can be determined later. The benefit of this sort of separation is that you can change individual parts without worrying about the whole. And once you get into testing globals become problematic.

One more note. If you're using globals for configuration, try instead to put your configuration either into a class, as suggested by Steini, or you can use a JSON file or a simple PHP file that returns an array. For example:

return [
    'setting1' => 'value'
];
mrberggg
  • 394
  • 1
  • 5
  • 13
-1

There are always things that have to be changed when you edit your code.

Renaming stuff is in the best case performed by a professional IDE with refactoring tools on board. Then you dont have to bother with both methods.

But it is very good that you care about avoiding redundant code and seach for better ways that is always a good approach!

However I personally prefer to use a config class for global variables using singleton pattern:

With this you also have the possibility to make a getter function for each case and make it more dynamic.

For example:

//config.php
final class Config {

    private static $instance = null;

    private $imagePath = "/style/images";

    private $sqlDatabaseName = "db-name";

    public function getImagePath($image = null) {
        $retVal = $this->imagePath;
        if(strlen($image) > 0) {
            $retVal .= "/" . $image;
        }

        return $retVal;
    }

    public function getSqlDatabaseName() {
        return $this->sqlDatabaseName;
    }

    public static function instance() {
        if(self::$instance === null) {
            self::$instance = new self();
        }

        return self::instance;
    }

    private function __construct() { }
    private function __clone() { }
}

Usage is simple:

//index.php
include("config.php");

function renderImage($imageName)
{
    return  '<img src="' . Config::instance()->getImagePath($imageName) . '" alt="img" />';
}

//Usage later in template:

<div class="bla blub">
    <?php echo renderImage("header.png"); ?>
</div>

Maybe you love this object orientated method as you have many possibilities to manipulate the behaviour of your return values with functions.

It also makes 'global' obsolete as you can simply import your single instance of $config basically everywhere aslong as you have included your file containing the config once.

Steini
  • 2,628
  • 13
  • 22