mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-04 23:23:37 +02:00
PHP7 Dependency Injection
This commit is contained in:
34
Structural/DependencyInjection/DatabaseConnection.php
Normal file
34
Structural/DependencyInjection/DatabaseConnection.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\DependencyInjection;
|
||||
|
||||
class DatabaseConnection
|
||||
{
|
||||
/**
|
||||
* @var DatabaseConfiguration
|
||||
*/
|
||||
private $configuration;
|
||||
|
||||
/**
|
||||
* @param DatabaseConfiguration $config
|
||||
*/
|
||||
public function __construct(DatabaseConfiguration $config)
|
||||
{
|
||||
$this->configuration = $config;
|
||||
}
|
||||
|
||||
public function getDsn(): 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 here
|
||||
|
||||
return sprintf(
|
||||
'%s:%s@%s:%d',
|
||||
$this->configuration->getUsername(),
|
||||
$this->configuration->getPassword(),
|
||||
$this->configuration->getHost(),
|
||||
$this->configuration->getPort()
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user