Last active
June 23, 2017 12:33
-
-
Save duskohu/ecc2664698c95c0dc2b5b0a32bd5fccd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pokial sa jedna o tento priklad tak connection by som riesil ako sluzbu samostatne a dal by som ju do DI, | |
pri registraci do DI by sa nakonfigurovala, naco sa ma stale vytvarat objekt DatabaseConnection staci jedn sluzba. | |
UserFacade by som tiez dal do DI nech si ju mozem zavolat kde potrebujem a DatabaseConnection si vyziadam, DI sa o to uz postara. | |
V reale by som asi pouzil doctrinu, lebo ma lepsie riesenu architekturu. | |
<?php | |
class DatabaseConnection { | |
/** @var string */ | |
protected $host; | |
/** @var string */ | |
protected $user; | |
/** @var string */ | |
protected $password; | |
... | |
/** | |
* @param string $host | |
* @param string $user | |
* @param string $password | |
*/ | |
public function __construct($host, $user, $password, ...) { | |
$this->host = $host; | |
$this->user = $user; | |
$this->password = $password; | |
... | |
} | |
... | |
} | |
class UserFacade { | |
/** @var DatabaseConnection */ | |
protected $databaseConnection; | |
/** | |
* @param DatabaseConnection $databaseConnection | |
*/ | |
public function __construct(DatabaseConnection $databaseConnection) { | |
$this->databaseConnection = $databaseConnection; | |
} | |
/** | |
* @param array $orderBy | |
* @return array | |
*/ | |
public function getUsers($orderBy = array()) { | |
$results = $this->databaseConnection->query('SELECT name FROM user ORDER BY ' . implode(',', $orderBy)); | |
return $results; | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment