[OOP] Simple question, which is better?
Moderator: General Moderators
[OOP] Simple question, which is better?
I have a Photo class and a Comment class. They have usual constructors to create or get from DB, edit methods, mass read, etc.
My question is: where I should put the method getCommentsForPhoto()? In the Photo class or in the Comment class as getCommentsForPhoto($photo_id)? Which is more correct?
My question is: where I should put the method getCommentsForPhoto()? In the Photo class or in the Comment class as getCommentsForPhoto($photo_id)? Which is more correct?
Re: [OOP] Simple question, which is better?
Just personal preference for me, but I'd probably put it with the Photo class.
Re: [OOP] Simple question, which is better?
I would as well. The comments are related to the photo, so they should be accessed via the Photo class. If you're going fully OOP though, the function shouldn't be called getCommentsForPhoto($photo_id), it should be:
Code: Select all
$Photo = new Photo($photo_id);
$comments = $Photo->getComments();Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: [OOP] Simple question, which is better?
Great. Thank you all 
Re: [OOP] Simple question, which is better?
This is exactly how I would have implemented it. Clean, clear, easy to maintain.pickle wrote:I would as well. The comments are related to the photo, so they should be accessed via the Photo class. If you're going fully OOP though, the function shouldn't be called getCommentsForPhoto($photo_id), it should be:
Code: Select all
$Photo = new Photo($photo_id); $comments = $Photo->getComments();
Re: [OOP] Simple question, which is better?
I would probably have the comment class extend the photo class...so it can access the same photoid and stuff.
Is that weird?
'lol
Is that weird?
'lol
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Re: [OOP] Simple question, which is better?
That's not how you're supposed to use the "extend" keyword. If you extend something it means you're taking something and building on it. So a square would extend a shape.jackpf wrote:I would probably have the comment class extend the photo class...so it can access the same photoid and stuff.
Is that weird?![]()
'lol
Re: [OOP] Simple question, which is better?
But surely the comments do build on the picture?
Say...a picture had certain permissions. If a user has commented, and wants to edit the comment, then you'll want to check the picture's permissions, along with the comment's.
Surely this would be easier:
Rather than:
I personally prefer the former.
Say...a picture had certain permissions. If a user has commented, and wants to edit the comment, then you'll want to check the picture's permissions, along with the comment's.
Surely this would be easier:
Code: Select all
public function check_permissions()
{
//get comment permissions...
if(!$this->picture_permissions() || !$comment_permissions)
{
//show some error...
}
}Code: Select all
public function check_permissions($photoid)
{
$picture = new picture($photoid);
$permissions = $picture->picture_permissions();
//get comment permissions...
if(!$permissions || !$comment_permissions
{
//show some error...
}
}Re: [OOP] Simple question, which is better?
In my opinion:
The Comments don't build on the Photo. They are not "extending" the Photo in the sense that: Comments = Photo + some others stuff. Semantically speaking, they do not need to be aware of everything in the universe that a Photo is. As far as a Comment is concerned, the universe consists of itself and the Photo it's associated with - and perhaps other Comments for the same Photos. Because of that, a Comment isn't extending a Photo.
The Comments don't build on the Photo. They are not "extending" the Photo in the sense that: Comments = Photo + some others stuff. Semantically speaking, they do not need to be aware of everything in the universe that a Photo is. As far as a Comment is concerned, the universe consists of itself and the Photo it's associated with - and perhaps other Comments for the same Photos. Because of that, a Comment isn't extending a Photo.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: [OOP] Simple question, which is better?
l0l...I wasn't really thinking about the definition of a photo and the semantics of comments in relation to photos.
I just thought it'd be easier to code
I just thought it'd be easier to code
Re: [OOP] Simple question, which is better?
I like jack, and always enjoy reading his discussions & posts... buuuuut, pickle & I are on the exact same page with regards to this scenariopickle wrote:In my opinion:
The Comments don't build on the Photo. They are not "extending" the Photo in the sense that: Comments = Photo + some others stuff. Semantically speaking, they do not need to be aware of everything in the universe that a Photo is. As far as a Comment is concerned, the universe consists of itself and the Photo it's associated with - and perhaps other Comments for the same Photos. Because of that, a Comment isn't extending a Photo.
Re: [OOP] Simple question, which is better?
Fair enough. I always find it easier to write & debug code when the code follows the semantics of what they're trying to represent. Especially in this case where an object can be mapped directly to a noun.jackpf wrote:I just thought it'd be easier to code
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.