I’m working on a little web-project-management-application.
I have a few classes like Clients(), Services(), Projects() etc.
Every class contains methods like getClient(), newClient(), editClient(), … and every on of this methods needs database connection. I’ve included an extra PDO class.
Current code of my clients class:
<?php
// include database class
require_once('../../../lib/php/classes/database/database.class.php');
class Client {
public function getClient() {
$db = Database::get("default");
$clients = $db->select("SELECT * FROM clients ORDER BY name ASC");
return $clients;
}
public function newClient($values) {
$db = Database::get("default");
if ($db->insert("INSERT INTO clients (name,initial,payment,hourly_rate,active) VALUES (?,?,?,?,?)",$values)) {
} else {
print "Inserting failed";
}
}
}
?>
But I think this makes no sense. How could i do it better? First idea: constructor method, that connects to the DB.
Everybody seems to love a bit of dependency injection these days, this will decouple your database object from the Client class.