Skip to content

Instantly share code, notes, and snippets.

@jrdmcgr
Created October 13, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrdmcgr/f97bb028dd55f172af1ea81937ad8891 to your computer and use it in GitHub Desktop.
Save jrdmcgr/f97bb028dd55f172af1ea81937ad8891 to your computer and use it in GitHub Desktop.
<?php
class Link
{
public function __construct($name, $url)
{
$this->name = $name;
$this->url = $url;
}
}
// Without DI
class ListOfLinks
{
public function __construct($database)
{
$this->database = $database;
$this->links = $this->fetch();
}
private function fetch()
{
$results = $this->database->select('select * from links');
$links = [];
foreach ($results as $link) {
$links[] = new Link($link['name'], $link['url']);
}
return $links;
}
}
// With DI
class ListOfLinks
{
public function __construct($database, $link_class)
{
$this->database = $database;
$this->link_class = $link_class;
$this->links = $this->fetch();
}
private function fetch()
{
$results = $this->database->select('select * from links');
$links = [];
foreach ($results as $link) {
$links[] = new $this->link_class($link['name'], $link['url']);
}
return $links;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment