Skip to content

Instantly share code, notes, and snippets.

@duskohu
Last active June 23, 2017 12:33
Show Gist options
  • Save duskohu/ecc2664698c95c0dc2b5b0a32bd5fccd to your computer and use it in GitHub Desktop.
Save duskohu/ecc2664698c95c0dc2b5b0a32bd5fccd to your computer and use it in GitHub Desktop.
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