How can the use of a Dependency Injection Container lead to cleaner and more organized code in PHP projects?

Using a Dependency Injection Container can lead to cleaner and more organized code in PHP projects by centralizing the management of object dependencies. This eliminates the need for manually instantiating objects and passing dependencies through constructors, resulting in more modular and testable code.

// Example of using a Dependency Injection Container in PHP

// Define a simple class
class Database {
    public function connect() {
        return 'Connected to database';
    }
}

// Create a DI container
class Container {
    private $instances = [];

    public function get($class) {
        if (!isset($this->instances[$class])) {
            $this->instances[$class] = new $class();
        }
        return $this->instances[$class];
    }
}

// Usage example
$container = new Container();
$database = $container->get('Database');
echo $database->connect();