What are the potential pitfalls of making a registry available in all classes in PHP?

Potential pitfalls of making a registry available in all classes in PHP include tight coupling between classes, making it harder to test and maintain code, as well as increasing the risk of global state issues. To solve this, consider using dependency injection to pass the registry instance only to the classes that need it, rather than making it globally accessible.

class Registry {
    private static $instance;
    private $data = [];

    private function __construct() {}

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

    public function set($key, $value) {
        $this->data[$key] = $value;
    }

    public function get($key) {
        return $this->data[$key] ?? null;
    }
}

class MyClass {
    private $registry;

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

    public function doSomething() {
        $value = $this->registry->get('key');
        // do something with $value
    }
}

$registry = Registry::getInstance();
$registry->set('key', 'value');

$myClass = new MyClass($registry);
$myClass->doSomething();