mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-22 22:39:36 +02:00
cs DependencyInjection
This commit is contained in:
parent
79f94ba501
commit
454382d8fb
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace DesignPatterns;
|
namespace DesignPatterns\DependencyInjection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dependency Injection
|
* Dependency Injection
|
||||||
@ -18,15 +18,20 @@ namespace DesignPatterns;
|
|||||||
*/
|
*/
|
||||||
class Configuration
|
class Configuration
|
||||||
{
|
{
|
||||||
protected $_host;
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $host;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $host
|
* @param string $host
|
||||||
|
*
|
||||||
* @return Configuration
|
* @return Configuration
|
||||||
*/
|
*/
|
||||||
public function setHost($host)
|
public function setHost($host)
|
||||||
{
|
{
|
||||||
$this->_host = $host;
|
$this->host = $host;
|
||||||
|
|
||||||
return $this; // for a fluent interface
|
return $this; // for a fluent interface
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,29 +40,6 @@ class Configuration
|
|||||||
*/
|
*/
|
||||||
public function getHost()
|
public function getHost()
|
||||||
{
|
{
|
||||||
return $this->_host;
|
return $this->host;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Connection
|
|
||||||
{
|
|
||||||
protected $_configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* here, Configuration gets injected and Connection will get all that it needs from Configuration
|
|
||||||
* without DI, the configuration would be created directly in Connection, which is not very good
|
|
||||||
* for testing and extending Connection
|
|
||||||
*
|
|
||||||
* @param Configuration $config
|
|
||||||
*/
|
|
||||||
public function __construct(Configuration $config)
|
|
||||||
{
|
|
||||||
$this->_configuration = $config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function connect()
|
|
||||||
{
|
|
||||||
$host = $this->_configuration->getHost();
|
|
||||||
// ...
|
|
||||||
}
|
}
|
||||||
}
|
}
|
35
DependencyInjection/Connection.php
Normal file
35
DependencyInjection/Connection.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\DependencyInjection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Connection
|
||||||
|
*/
|
||||||
|
class Connection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Configuration
|
||||||
|
*/
|
||||||
|
protected $configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* here, Configuration gets injected and Connection will get all that it needs from Configuration
|
||||||
|
* without DI, the configuration would be created directly in Connection, which is not very good
|
||||||
|
* for testing and extending Connection
|
||||||
|
*
|
||||||
|
* @param Configuration $config
|
||||||
|
*/
|
||||||
|
public function __construct(Configuration $config)
|
||||||
|
{
|
||||||
|
$this->configuration = $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* connection using the injected config
|
||||||
|
*/
|
||||||
|
public function connect()
|
||||||
|
{
|
||||||
|
$host = $this->configuration->getHost();
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user