PHP class design with methods requiring database access

I've been a procedural PHP programmer for a long time but I'm getting to learn OOP and would like some advice. I'm designing a class which currently is composed mainly of simple getters/setters, but I'm not quite sure if I'm designing my class in the best way possible.

The getters require accessing a database to pull the requested information, so I'm checking if a property has been set first before running the query which seems like a good idea to me but I'm not sure.

I think I could make use of DI by type hinting the $connect parameter and using a DAL but this is not a public facing API and we're never going to use a different database engine so I'm not sure it's worth the trouble. I'm using a proprietary database engine which has a bug preventing me using PDO and parametrised queries. I'm also not sure if I should be try/catching/throwing exceptions inside class methods. Here is a sample of the code below, there are more methods/properties but they all follow the same basic structure. I would very much appreciate any and all commentary. class Patient { // Database connector private$connect;
protected $_connection; public$dmg_ID;
public $LNK_ID; public$dailyLNK_ID;

public function __construct($connect,$lnkid, $dlkid = NULL) {$this->_connection = $connect; unset($connect);

// Patient requires a LNK_ID but not always a daily link ID
$this->LNK_ID =$lnkid;

if (!is_null($dlkid) && is_numeric($dlkid)) {
$this->dailyLNK_ID =$dlkid;
}
}

public function getDmgID() {

if ($this->dmg_ID) return$this->dmg_ID;

$sql = "SELECT lnk_dmgID FROM LINK WHERE lnk_ID=".$this->LNK_ID;
try {
$result = odbc_exec($this->_connection,$sql); if($result) {
$dmgID = odbc_fetch_array($result);
}
else {
throw new RuntimeException("Failed to connect.");
}
}
catch (RuntimeException $e) { print("Exception caught:$e");
}

return $this->dmg_ID =$dmgID['LNK_DMGID'];
}

public function setDmgID() {

if (!$this->dmg_ID) return false;$sql = "UPDATE LINK SET lnk_dmgID=".$this->dmg_ID." WHERE lnk_ID=".$this->LNK_ID;
try {
$result = odbc_exec($this->_connection,$sql); if(!$result) {
throw new RuntimeException("Failed to connect.");
}
}
catch (RuntimeException $e) { print("Exception caught:$e");
}

return true;
}
...


From a look at your code, I am a bit confused by the naming connect and connection and the latter stores $connect in the constructor. Maybe give a more descriptive names (one of the top recommendations in the mentioned book). • Why are you unsetting $connect?
• The names such as $LNK_ID are cryptic for reader. • I would reserve a separate class dealing with the database via thin interface (see my post) and let other classes only talk to that class instead of DB directly. • Thanks for the response. This is for a company that is very heavily invested in the current database engine and cannot change without rewriting their entire software suite. Would not building a DAL in this instance violate YAGNI? I'm unsetting$connect because it contains the data necessary to connect to the DB and was taught it's good practice to get unset once it's no longer needed. \$LNK_ID is literally the name of the database field it refers to although I do agree the mixing of upper/lower case with/without underscores is confusing. Thanks for your responses :) – Roy Feb 6 '14 at 14:56
• @Roy unset can be useful to quickly remove keys (or move to different keys) on your model (or its clone) before passing it to persistent storage. Or whenever you want to remove keys of objects or assoc. arrays. – Dmitri Zaitsev Feb 7 '14 at 12:38