! class EmployeeRepo {! ! public function __construct() {! ! ! global $db;! ! ! $this->db = $db;! ! }! ! ! public function getAllEmployees() {! ! ! return $this->db->query('SELECT id, name FROM employee');! ! }! }!
! include 'db_setup.php';! include 'employee_repo.php';! ! $repo = new EmployeeRepo;! $data = $repo->getAllEmployees();! include 'employee_view.php';! ! include 'db_teardown.php';! ?>!
! include ‘vendor/autoloader.php’;! include ‘db_setup.php’;! ! $repo = new EmployeeRepo;! $data = $repo->getAllEmployees();! include 'employee_view.php';! ?>!
! class EmployeeRepo {! ! public function __construct() {! ! ! global $db;! ! ! $this->db = $db;! ! }! ! ! public function getAllEmployees() {! ! ! return $this->db->query('SELECT id, name FROM employee');! ! }! }!
! class EmployeeRepo {! ! public function __construct($db) {! ! ! $this->db = $db;! ! }! ! ! public function getAllEmployees() {! ! ! return $this->db->query(‘SELECT id, name FROM employee');! ! }! }!
! include ‘vendor/autoload.php';! include ‘db_setup.php’;! ! $repo = new EmployeeRepo($db);! $data = $repo->getAllEmployees();! include 'employee_view.php';! ?>!
“Don’t Repeat Yourself” was never about code. It’s about knowledge. It’s about cohesion. If two pieces of code represent the exact same knowledge, they will always change together. Having to change them both is risky: you might forget one of them. On the other hand, if two identical pieces of code represent different knowledge, they will change independently. De-duplicating them introduces risk, because changing the knowledge for one object, might accidentally change it for the other object. - http://verraes.net/2014/08/dry-is-about-knowledge/