What are the best practices for passing global scope variables to objects in PHP?

When passing global scope variables to objects in PHP, it is best practice to use dependency injection. This involves passing the variables as parameters in the object's constructor method. This approach helps to keep the code modular, testable, and easier to maintain.

<?php

// Global scope variable
$globalVar = 'Hello, world!';

// Object that requires access to global variable
class MyClass {
    private $globalVar;

    public function __construct($globalVar) {
        $this->globalVar = $globalVar;
    }

    public function getGlobalVar() {
        return $this->globalVar;
    }
}

// Instantiate object with global variable passed in
$myObject = new MyClass($globalVar);

// Access the global variable within the object
echo $myObject->getGlobalVar(); // Output: Hello, world!

?>