depz123
depz123

Reputation: 507

Creating a user profile using OOP PHP

I am creating a profile page for a user (just like the one in this forum).

I have a class like this

class User
{
    private $db;

    private $name;
    private $location;
    private $email;
    private $age;
    private $photo;

    function __construct($player_id, DB $db)
    {
        $this->db = $db;
        $stm = $this->db->prepare_and_execute(
            'SELECT * from users
             where id = ?',
             $_SESSION['user_id']
            );
        $result = $stm->fetch();
        $this->email = $result['email'];

        $stm = $this->db->prepare_and_execute(
            'SELECT * from user_details
             where user_id = ?',
             $_SESSION['user_id']
            );
        $result = $stm->fetch();

        $this->name = $result['name'];
        $this->age = $result['age'];
        $this->photo = $result['photo'];

    }

    public function get_player_name()
    {
        return $this->name;
    }

    public function get_player_location()
    {
        return $this->location;
    }

    public function get_player_age()
    {
        return $this->age;  
    }

    // and so on...
}

In my profile.php page where the details of the user are being displayed i have something like this:

<?php
    $user = new User($_SESSION['user_id'], $db);
?>

<table id="info_table">
    <tr>
        <td class="td-align-top">Info</td>
        <td class="td-align-top">
            <table>
                <tr><td>Location</td></tr>
                <tr><td>Age</td></tr>
                <?php if ($user->is_own_profile()) {
                    // check whether the user is viewing
                    //is his own profile or another user's profile ?>
                    <tr><td>Email</td></tr>
                <?php } ?>
                <tr><td>Organization</td></tr>
            </table>
        </td>
        <td class="td-align-top">
            <table>
                <tr><td><?php echo $player->get_player_location(); ?></td></tr>
                <tr><td><?php echo $player->get_player_age(); ?></td></tr>
                <?php if ($user->is_own_profile()) { ?>
                    <tr><td><?php echo $player->get_player_email(); ?></td></tr>
                <?php } ?>
                <tr><td><?php echo $player->get_player_organization(); ?></td></tr>
            </table>
        </td>
    </tr>
</table>

So i have 2 questions:

  1. As you can see i am using a function called is_own_profile() to check whether the user is viewing his own profile or he is on some other users profile and based on the result of that condition, i am hiding/ showing the email address. Now as you can imagine i will have to use the same if-else condition for deciding whether to show or hide a lot of other things on the page like date of bith, Edit option etc..so instead of having this if-else constructs all over the page is there a cleaner way to do this?

  2. At the beginning of the profile.php page, i am creating an user object and populating it with some data from the db. Now how can i make this object available in other pages like notifications, messages etc. instead of instantiating and populating the object on each and every page. i have considered putting the object in a session but after going through some answers in this forum i realized that it is best to avoid putting too much data in a session. So what is the best way to approach this?

Thanks..

Upvotes: 0

Views: 3198

Answers (1)

kalley
kalley

Reputation: 18462

If you don't want to save it in a session, you could just make an include file and have the instantiation there. That way if there is anything else that you need to do on each page, all you have to do is add it there.

Also, for your if-else thing, you could do something like the following:

$player->show_own('email', '<tr><td>', '</td></tr>');

where get_player_email looks something like this:

public function show_own($prop, $before = '', $after = '')
{
    if ( $this->is_own_profile() ) {
        return $before . $this->$prop . $after;  
    }
    return '';
}

Then you can reuse that method for other properties as well.

Upvotes: 3

Related Questions