What are the potential pitfalls of using the Registry Pattern in PHP for global object access?

One potential pitfall of using the Registry Pattern in PHP for global object access is that it can lead to tight coupling between objects and make the code harder to maintain and test. To solve this issue, consider using dependency injection instead to pass objects where they are needed.

class Registry {
    private static $objects = [];

    public static function set($key, $object) {
        self::$objects[$key] = $object;
    }

    public static function get($key) {
        return self::$objects[$key];
    }
}

class MyClass {
    private $dependency;

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

    public function doSomething() {
        // Use the dependency here
    }
}

$dependency = new Dependency();
Registry::set('dependency', $dependency);

$myClass = new MyClass(Registry::get('dependency'));
$myClass->doSomething();