How can the Registry pattern help reduce the need for passing objects through multiple layers of classes in PHP?

The Registry pattern can help reduce the need for passing objects through multiple layers of classes in PHP by providing a centralized storage for objects that can be accessed globally. This way, objects can be stored in the registry once and accessed from any part of the application without the need to pass them through each layer of classes.

class Registry {
    private static $objects = [];

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

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

// Usage example
$user = new User();
Registry::set('user', $user);

// In another part of the application
$user = Registry::get('user');
$user->doSomething();