How can Dependency Injection be configured for practical use in PHP applications?

Dependency Injection in PHP applications can be configured by creating a container that manages the dependencies and injects them into classes when needed. This helps in decoupling classes and making them more testable and maintainable.

// Container class to manage dependencies
class Container {
    private $dependencies = [];

    public function __construct() {
        $this->dependencies['db'] = new Database(); // Example dependency
    }

    public function getDependency($key) {
        return $this->dependencies[$key];
    }
}

// Example class using dependency injection
class UserRepository {
    private $db;

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

    public function getUsers() {
        return $this->db->query('SELECT * FROM users');
    }
}

// Usage
$container = new Container();
$userRepository = new UserRepository($container->getDependency('db'));
$users = $userRepository->getUsers();