How can dependencies be injected into PHP objects across multiple files?

When injecting dependencies into PHP objects across multiple files, one common approach is to use a dependency injection container. This container can manage the instantiation and injection of dependencies into objects, allowing for easier management and decoupling of components.

// File: Container.php

class Container {
    private $dependencies = [];

    public function __construct(array $dependencies = []) {
        $this->dependencies = $dependencies;
    }

    public function get($name) {
        if (isset($this->dependencies[$name])) {
            return $this->dependencies[$name]();
        }
        throw new Exception("Dependency not found: $name");
    }

    public function set($name, Closure $resolver) {
        $this->dependencies[$name] = $resolver;
    }
}