1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Best Practice: Where to put methods

Discussion in 'PHP' started by stats, Oct 27, 2011.

  1. #1
    suppose i have 2 classes, 'User' and 'Image'

    a user can have many images. I need to get all images that belong to a given user.

    Which of the following options is a more correct way to go ?

    Option 1:
    $user = new User();
    $userImages = $user->getAllImages('myusername');
    PHP:
    Option 2:
    $image = new Image();
    $userImages = $image->getImagesByUsername('myusername');
    PHP:
    I know that both of these are going to work just fine, just want to know what is the general principle to decide in this kind of situations ?

    as per my thoughts .. I have 2 thoughts which contradict ..
    1.) since the argument is a user, it should be in User class
    2.) since the return is image(s), it should be in Image class


    Please lemme know which option would you choose and why ?

    Thanks
     
    stats, Oct 27, 2011 IP
  2. SheetalCreation

    SheetalCreation Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #2
    Hi,

    You should put all image related functions in image class.... all function which manipulate their class specific data or return their class specific data should come under same class, It can take input as a other other class param.

    Sheetal G
     
    SheetalCreation, Oct 27, 2011 IP
  3. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #3
    I would add it within the User class since your start point is the User.

    If it was within the Image class, you'd need to first instantiate an Image object, THEN load the Image(s) for a User, which doesn't make sense. (unless you add a static method to your Image class which saves you having to instantiate it)

    Any Image specific methods should live in the Image class. (so resize(), crop(), rotate() etc)
     
    mfscripts, Oct 28, 2011 IP
  4. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #4
    ok so far we got 2 contradicting answers (just like my initial 2 thoughts) .. :)

    well .. maybe someone more literated had read something about this somewhere on the "big-project" descriptions ? I mean .. does anyone know which strategy the Zend framework goes with, Cake, Wordpress, Joomla, or others ? I believe they should have a set rule for these kind of situations ?
     
    stats, Oct 28, 2011 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    There are several factors to be considered here:

    1. Relationship:
    Here, User "has" Images and Images "belongs to" User are the relationships. Thus giving indication to have the functionality to fetch images for a given user should be within User class.

    2. Business Logic:
    There may be business logic behind fetching user images, like if user is active / non-active, if paid subscription has expired or not. Such logics would make more sense to have within User class rather than image class to check over passed parameter.

    3. Aggregation:
    Image is more of a generic and is used for a User, thus User is higher level entity aggregating multiple Images. Another reason to have the functionality within User class.


    Above are my 2 cents, and decision making may vary from person to person.

    By the way, in your first example, passing username as parameter to the method won't make any sense as username / other user related details are already within the object.
     
    mastermunj, Oct 29, 2011 IP