<?phpdeclare(strict_types=1);// php 技术群:781742505classDatabaseConfiguration{/**
* @var string
*/private$host;/**
* @var int
*/private$port;/**
* @var string
*/private$username;/**
* @var string
*/private$password;publicfunction__construct(string $host, int $port, string $username, string $password){$this->host=$host;$this->port=$port;$this->username=$username;$this->password=$password;}publicfunctiongetHost(): string
{return$this->host;}publicfunctiongetPort(): int
{return$this->port;}publicfunctiongetUsername(): string
{return$this->username;}publicfunctiongetPassword(): string
{return$this->password;}}classDatabaseConnection{/**
* @var DatabaseConfiguration
*/private$configuration;/**
* @param DatabaseConfiguration $config
*/publicfunction__construct(DatabaseConfiguration $config){$this->configuration=$config;}publicfunctiongetDsn(): string
{// this is just for the sake of demonstration, not a real DSN// notice that only the injected config is used here, so there is// a real separation of concerns herereturnsprintf('%s:%s@%s:%d',$this->configuration->getUsername(),$this->configuration->getPassword(),$this->configuration->getHost(),$this->configuration->getPort());}}$config=newDatabaseConfiguration('localhost',3306,'domnikl','1234');$connection=newDatabaseConnection($config);echo$connection->getDsn();// domnikl:1234@localhost:3306