SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with object relationships

    I am designing an intranet site for my office. It will require users to login and am having some trouble with how objects will relate. I am trying to keep it as OO as possible. Here are some requirements.

    Site is always accessed by user (user table, user object)
    User is always a person (user extends person)
    User is sometimes an employee
    Employee is always a user and person (employee extends user, which extends person)

    My confusion is since I'm always dealing with a user (userID in session), how do I handle the instances when the user is an employee. I'm thinking that instead of employee extending user, maybe the user should contain an employee object (if user is not employee, null).

    Thanks,
    Steve

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a classic. Let the person own one or more roles. Let user and employee both extend role.

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, I think I understand. But if user and employee extend role, how will I get an employee if all I have is user.

  4. #4
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, I came across this pdf on the same topic. http://st-www.cs.uiuc.edu/users/hanm...ngs/riehle.pdf.
    I'll study it and come back here if I need more help.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class Person
    {
        protected 
    $roles = Array();

        function 
    getRoles() {
            return 
    $this->roles;
        }

        function 
    hasRole($role) {
            foreach (
    $this->roles as $r) {
                if (
    $r == $role) {
                    return 
    true;
                }
            }
            return 
    false;
        }

    PHP Code:
    class Anonymous extends Person
    {
    }
    class 
    Ssettlemyre extends Person
    {
        function 
    __construct() {
            
    $this->roles[] = "employee";
            
    $this->roles[] = "user";
        }

    PHP Code:
    $p1 = new Anonymous();
    echo 
    $p1->hasRole("employee"); // false

    $p2 = new Ssettlemyre();
    echo 
    $p2->hasRole("employee"); // true 
    There are two advantages of this design over using inheritance. The most obvious is that you now have a many-to-many relationship between person and role, where inheritance would restrict you to a many-to-one. The other advantage is that the relationship is dynamic - it can change during execution. So you could load the roles from a database, or even change them during execution.

    (I replaced Role with a string in the above example, but it would hold true even if you used an object as datatype for role)

  6. #6
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, now i see. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •