PHP7 Dependency Injection

This commit is contained in:
Dominik Liebler
2016-09-23 10:12:59 +02:00
parent 1f9348d9a8
commit e6c67c5da5
12 changed files with 694 additions and 643 deletions

View 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()
);
}
}