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
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
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)
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 ?
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.