Created
October 13, 2016 16:24
-
-
Save jrdmcgr/f97bb028dd55f172af1ea81937ad8891 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
<?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