Page 1 of 2
[OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 11:56 am
by lorenzo-s
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?
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 12:19 pm
by Mirge
Just personal preference for me, but I'd probably put it with the Photo class.
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 12:47 pm
by jackpf
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 2:05 pm
by pickle
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?
Posted: Mon Sep 21, 2009 2:07 pm
by lorenzo-s
Great. Thank you all

Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 2:49 pm
by Mirge
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();
This is exactly how I would have implemented it. Clean, clear, easy to maintain.
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 2:56 pm
by jackpf
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?
Posted: Mon Sep 21, 2009 3:03 pm
by jayshields
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
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.
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 3:12 pm
by jackpf
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:
Code: Select all
public function check_permissions()
{
//get comment permissions...
if(!$this->picture_permissions() || !$comment_permissions)
{
//show some error...
}
}
Rather than:
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...
}
}
I personally prefer the former.
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 3:38 pm
by pickle
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?
Posted: Mon Sep 21, 2009 3:59 pm
by jackpf
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

Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 4:05 pm
by Mirge
pickle 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.
I like jack, and always enjoy reading his discussions & posts... buuuuut, pickle & I are on the exact same page with regards to this scenario

.
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 4:09 pm
by jackpf
Traitor

Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 4:13 pm
by Mirge
Re: [OOP] Simple question, which is better?
Posted: Mon Sep 21, 2009 4:46 pm
by pickle
jackpf wrote:I just thought it'd be easier to code
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.