If you are using an mvc to build a user profile, would it be better to have the conditional statements used to work out the display type of the comments within a function in the model or in the controller like this:
For example I have 3 classes
- Comments
- Member
- Admin (extends member)
Some example make use of Pseudo code where functions are missing
Option 1
Dependant of the type of user logged in the showComments
function returning the comments would give back different information.
class user {
function isLoggedIn() { //Check if a user is logged in }
function getUserType() { // return user type }
function showComments($id) { //comments code }
}
class admin extends user {
function showComments($id) { //comments code }
}
Then use code within the controller to determine dependant upon the user type logged in which to show?
$profileContent = $user->getOtherContent();
if ($user->isLoggedIn() && $user->getUserType() == "member") {
$member = new member();
$comments = $member->showComments($profileId);
}
elseif ($user->isLoggedIn() && $user->getUserType() == "admin") {
$admin = new admin();
$comments = $admin->showComments($profileId);
}
else
$comments = $user->showComments($profileId);
require 'templates/profile.php';
Option 2
As this is a custom framework I could move everything into a function within the model and have one function in user to check the comment type to display:
abstract class user {
function isLoggedIn() { //Check if a user is logged in }
function getUserType() { // return user type }
}
class profile {
function showComments($profileId, $user) {
if (User::isLoggedIn() && User::getUserType() == "member") {
$comments = //database query and formatting for member
}
elseif (User::isLoggedIn() && User::getUserType() == "admin") {
$comments = //database query and formatting for admin
}
else
$comments = //database query and formatting for guest
return $comments;
}
}
Using a controller like:
$profile = new profile($profileId);
$comments = $profile->showComments();
require 'templates/profile.php';