OO and inheritance

Hello All!

I’ve put off learning PHP for long enough and decided to get stuck in. Im looking at OO right now and am having a problem with inheritance, or maybe its just a design issue?

I’ve got two tables in my database on for people and one for users. The users table has a foreign key linking back to the relevant person.

have a Person class and a User class that extends Person.

When I create a new person object Im grabbing the record from the database and passing it to a protected function…

[php]
public function fetchById($id) {
… DO SQL…
$this->populatePerson($pData)
}
final protected function populatePerson($pData) {
$this->setId($pData[‘id’]);
$this->name($pData[‘name’]);
}
protected function setId($id) {
$this->id = $id;
}
[/php]

I want to be able to get that id with $person->getId() which I can, everything works fine.

My User class looks like this…

[php]
public function fetchById($uid) {
… DO SQL …
parent::fetchById($uData[“pid”]);
$this->populateUser($uData);
}

final protected function populateUser($uData) {
$this->setId($uData[‘id’]);
}

protected function setId($id) {
$this->userid = $id;
}

public function getId() {
return $this->userid;
}

public function getPersonId() {
parent::getId();
}
[/PHP]

It seems to make sense to me that when you call getId() on the user object that you should get the user id but it looks like when I call parent::fetchById($uData[“pid”]); from the user object, the setId that should then be called in the parent is actually being called from the User class.

Now I understand that I can change the populatePerson method from

[php]
$this->setId($pData[‘id’]);
[/php]

to

[php]
self::setId($pData[‘id’]);
[/php]

What im not sure about is whether thats the right/best way to do it. If so do I declare all of the calls in there to self::?

Sponsor our Newsletter | Privacy Policy | Terms of Service