简体   繁体   中英

How to design these classes correctly?

Im wondering about how to go about this. I have a class User.class.php that goes something like this

class User {
      protected $db;
      protected $username;
      protected $userid;
      protected $firstName;
      protected $lastName;

      public function __construct(PDO $db, $username){
      }

      public function editProfile($userDetails){
      }

      //getter and setters
}

This class is used for the users profile.php page so the user must be logged in for this to work. Notice __construct(PDO $db, $username)

So I have another page members.php that will show a list of users. There will be people who are not logged in visiting this page so User.class cannot be used so im thinking about creating another class called Members.class but the thing is it will have duplicated content from User.class

Should I have a User.class for logged in users and Member.class for viewing other users? Or should i redesign User.class to allow for both? I know this solution could be simple but due to my limited experience i want to know which is the better way to go about it? Please and Thanks

EDIT: I'll explain more...

profile.php

Have to be logged in.

<?php
//Instantiate User
$user = new User($db, $session->get('username'));
?>
<html>
<body>
       <strong>My Profile</strong>
       <?php echo $user->firstName(); ?><br />
       <?php echo $user->lastName(); ?><br />
       <a href="editprofile">Edit Profile</a>
</body>

members.php

Showing a list of members. Visitor may not be logged in and because of this i will get an error trying to instantiate the User class because there is no session

<?php
//Instantiate User
$user = new User($db, $session->get('username'));
?>
<html>
<body>
     <?php
         //FOR LOOP to loop through users from database
         //echo users firstname and lastname
     ?>
</body>
</html>

This class is used for the users profile.php page so the user must be logged in for this to work.

Is this what you want your user class to do? Do you want to have an instance of the user class only created when they are being logged in, or should the class represent that user's record with the site? IMHO user should be a class that represents a user that can be used for whatever purposes necessary by other classes. Profile.php can just show the data of the user, no need to make the user class specifically accustomed to profile.php

Should I have a User.class for logged in users and Member.class for viewing other users?

If members.php is a page for displaying users, it seems like it should be using the user class to do so instead of being its own separate data entity. Is a member different from a user in that one is logged in and one is not? This doesn't seem to closely match up the 'real world'. Being logged in is a state of a user, which could easily be maintained by having a list of users that are logged in.

Or should i redesign User.class to allow for both?

If you have to redesign User to allow for both, then maybe User has too many responsibilities and should be simplified. The class should contain the info of a user, and have some basic methods for its usage by other classes, not do everything itself. It should be easy to have a list of instances of the user class (or their ids or some representation of a specific user) that lets you view the info for those that are currently online.

Another solution would be to let Member inherit User .

class User {
  // Common methods and propertis
}
class Member extends User {
  // Special member stuff
}

This way you can think about another class "Guest"

Based on your edit, it seems like you shouldn't be using more than a single class for what you're doing, since it represents a logical user entity.

Why are you trying to instantiate an instance of User in members.php (assigning it to $user )? What exactly would this user correspond to, logically, if no user is logged in? What would you want to do with it?

It seems like what you want is something like this:

<?php

$users = User::getUsers(); // Or something similar.

?>
<html>
<body>
<?php

foreach ($users as $user)
{
    // Display the user; for example:
    echo $user->name . '<br />';
}

?>
</body>
</html>

In this case, you'd implement the static method User::getUsers to retrieve the complete list of users for you to display.

You should separate the user model and the login/session logic. Create a UserSession, that will handle authentication and stores user data as needed in a session.


class User {
    // has credentials and other information
}

class UserSession {
    //login will only be used when the user logs in
    function login(User $user){}
    function isLoggedIn(){}
    //either the username from User or guest/anon if not logged in
    function getUsername(){}
}

if ($userSession->isLoggedIn()){
    // allow access to profile.php
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM