What is the purpose of a Dependency Injection Container in PHP and how can it be implemented?

A Dependency Injection Container in PHP is used to manage the dependencies of an application by providing a centralized way to create and manage objects. This helps in decoupling the components of an application, making it more modular and easier to maintain.

// Implementing a simple Dependency Injection Container in PHP

class Container {
    private $dependencies = [];

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

    public function get($key) {
        if (isset($this->dependencies[$key])) {
            return $this->dependencies[$key];
        }
        return null;
    }
}

// Example usage
$container = new Container();

$container->set('db', new Database());
$container->set('logger', new Logger());

$db = $container->get('db');
$logger = $container->get('logger');