[OOP] Simple question, which is better?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
lorenzo-s
Forum Commoner
Posts: 43
Joined: Tue Aug 25, 2009 12:25 pm

[OOP] Simple question, which is better?

Post 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?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [OOP] Simple question, which is better?

Post by Mirge »

Just personal preference for me, but I'd probably put it with the Photo class.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [OOP] Simple question, which is better?

Post by jackpf »

:yar:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [OOP] Simple question, which is better?

Post 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();
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
lorenzo-s
Forum Commoner
Posts: 43
Joined: Tue Aug 25, 2009 12:25 pm

Re: [OOP] Simple question, which is better?

Post by lorenzo-s »

Great. Thank you all :)
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [OOP] Simple question, which is better?

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [OOP] Simple question, which is better?

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: [OOP] Simple question, which is better?

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [OOP] Simple question, which is better?

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [OOP] Simple question, which is better?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [OOP] Simple question, which is better?

Post 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 :P
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [OOP] Simple question, which is better?

Post 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 :).
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: [OOP] Simple question, which is better?

Post by jackpf »

Traitor :P
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: [OOP] Simple question, which is better?

Post by Mirge »

:drunk:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [OOP] Simple question, which is better?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply