Allright, I can understand your reasoning. I was not trying to model my classes after my tables but indeed, most of the time they are very much related. I hope you can point me towards a better solution. Here's the complete situation:
People (e.g. we want to store first name, last name, email) belong to groups (e.g. groups have a name, a single e-mailaddress and some other properties ). One person can be a member of multiple groups. The membership has a status (approved or not yet) and a time untill when it's valid.
In my database, I would map this to 3 tables: persons (personID, firstname, lastname, email), groups (groupID, name, email) and memberships (personID, groupID, status, validuntill).
Now is the question: what classes would you define? 2 or 3? And if you only take two: how would you handle the membership of groups?
Logically, you could choose for 2 classes: person and group.
class person {
function addToGroup($group) {
/* this function would take a group object to know to what group we want to add the person */
$sql = "insert into memberships (...) values ($this->id, $group->id, 'REQUESTED')";
... exec query ...
}
function changeMembershipStatus($group, $newStatus) {
$sql = "update memberships set status = '$newStatus' where groupID=$group->id and personID=$this->id";
... exec query ...
}
}
class group {
function addPerson($person) {
/* this function would take a person object to store */
$sql = "insert into memberships (...) values ($person->id, $this->id, 'REQUEST'";
... exec query ...
}
function changeMembershipStatus($person, $newStatus) {
$sql = "update memberships set status = '$newStatus' where groupID=$this->id and personID=$person->id";
... exec query ...
}
}
What I'm afraid of is the doubling of the code you start to see here ... I'm writing exactly the same funcitons for the group class as for the person class. though logically more correct, I figured this could not be the purpose. So I fell back to 3 classes:
class membership {
function membership (or __construct in PHP5) ($person = false, $group=false) {
if ($person) { $this->person = $person; }
if ($group) { $this->group = $group; }
}
function create () {
/* this function takes a person and a group object */
$sql = "insert into memberships (...) values ($this->person->id, $this->group->id, 'REQUEST'";
... exec query ...
}
function update ($newStatus) {
$sql = "update memberships set status = '$newStatus' where groupID=$this->group->id and personID=$this->person->id";
... exec query ...
}
}
What do you think? Looks better? How would you solve this problem?
As to what I would like to do with my group membership: I obviously want to list the groups a person belongs to and the persons which belong to a particular group. I want to list the persons who have requested a groupmembership. I want to list those persons whose membership is about to expire. And per person: I want to list the groups requiring attention (membership about to expire) and the one awaiting approval of membership, ...