Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 497043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:44:38+00:00 2026-05-13T05:44:38+00:00

how do you design your objects lets say you have an object user and

  • 0

how do you design your objects lets say you have an object user and an object access_role. The user is stored in users table in an sql database, the access_role knows the user_id and is stored in an sql database.

    class user() {

    protected $_name;
    protected $_id;
    protected $_age;
    protected $_password;

    public function getName();
    public function getId();
    public function getAge();
    public function getPassword();

    public function setName();
    public function setId();
    public function setAge();
    public function setPassword();

    //sql query
    public function read(){}; 


    }

    class accessRole {

    protected $_accessRole;
    protected $_userId;

    public function setAccessRole();
    public function setUserId();
    public function getAccessRole();
    public function getUserId();

    }

If i want the access role for the user in do an query in sql. But how do you connect both objects, so that something like:

$u = new User();
$u->getAccessRole();

will work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:44:38+00:00Added an answer on May 13, 2026 at 5:44 am

    I’d separate the base user and roles class by having an intermediator between them.

    $roles = $provider->roles($user);
    

    This way class user and class accessRole do not have to know each other or any of their respective properties. class usercan happily live without access roles and class accessRole isn’t bound to $_userId. E.g. what would the userid be for an accessRole soley based on the time of day? (Ok, silly example. Hope you get the point). You may even translate roles for various subsystems without the need to carry all the specific (sub-)roles throughout the system.

    $roles = $provider->convert($rolesBasedOnUser);
    

    edit: (hopefully) short example….
    I’m using a simple User class (no further interfaces or other kinds of abstraction) and there’s no Roles “class”, just a simple string array (for brevity’s sake). But keep in mind that you can do a lot more decoupling …if you like.

    class User {
      protected $id, $name;
      public function __construct($id) {
        $this->id = $id;
        $this->name = 'user #'.$id;
      }
    
      public function getName() {
        return $this->name;
      }
      public function getId() {
        return $this->id;
      }
    }
    
    interface UserRolesProvider {
      public function roles(User $user);
    }
    

    Now you can have code that relies on the user class and the UserRolesProvider interface but let’s you change the implementation of the provider without touching all the other code. Or it even only relies on the collection of Roles which let’s you build/create/assemble the Roles as you like. Ok, let’s have two (dummy) implementations of the provider interface.

    class UserRolesA implements UserRolesProvider {
      public function roles(User $user)
      {
        return 0===$user->getId()%2 ? array() : array('X');
      }
    }
    
    class UserRolesB implements UserRolesProvider {
      public function roles(User $user)
      {
        return 0!==$user->getId()%2 ? array() : array('X');
      }
    }
    

    and some User objects

    // "create" 6 users
    $users = array_map( function($x) { return new User($x); }, range(1,6));
    

    and something that uses both

    function foo($users, UserRolesProvider $provider) {
        foreach($users as $u) {
            $roles = $provider->roles($u);
            echo $u->getName(), ' ', join(',', $roles), " | ";
        }
        echo "\n";
    }
    foo($users, new UserRolesA);
    foo($users, new UserRolesB);
    

    prints

    user #1 X | user #2  | user #3 X | user #4  | user #5 X | user #6  | 
    user #1  | user #2 X | user #3  | user #4 X | user #5  | user #6 X | 
    

    Not too impressive. But the point is you don’t have to touch the User class or the code in function foo().
    And the decoupling can go on and on …if you like.
    E.g. a registry that can answer to “I have some [type] here. Can you give me something that builds roles from it?”
    Or maybe even “Can you build me a stub so that I can call fn($roles) as fn([type])?”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.