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

  • SEARCH
  • Home
  • 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 9123135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:15:57+00:00 2026-06-17T06:15:57+00:00

I am searching for a architectural solution in instantiating different child classes based on

  • 0

I am searching for a architectural solution in instantiating different child classes based on an object type or extending Base class with the methods of child classes.

To give an example:
There is a base class User and several child classes Partner, Client, Moderator which have specific methods an their own constructors.
When I am calling

$user = new User($userid);

I want User class

class User
{
  public function __construct($userid) {
    self::initDB(); 

    if ($this->isPartner()) {
        //extend this class with the methods of "Partner" child class and run "Partner" class constructor
    }

    if ($this->isClient()) {
        //extend this class with the methods of "Client" child class and run "Client" class constructor
    }

    if ($this->isModerator()) {
        //extend this class with the methods of "Moderator" child class and run "Moderator" class constructor
    }
  }
}

To return me an object with all of the methods depending on what roles does user have.

I know my logic is broken somewhere and the example I provided is wrong. But the only solution I see now is to build one giant class that has all of the methods for all of the roles – which looks like a mess.

  • 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-06-17T06:15:59+00:00Added an answer on June 17, 2026 at 6:15 am

    First of all, your database logic should be totally separate from your domain objects (User, etc). Otherwise you are violating the single responsibility principle (SRP).

    Set up your classes something like the following (base class User and multiple subclasses):

    class User 
    {
      private $id;
      // getters and setters go here
    }
    
    class Moderator extends User {}
    class Partner extends User {}
    // etc
    

    Then, create some sort of UserManager class that implements an interface that looks like the following:

    interface UserManagerInterface {
      function loadUserById($id);
    }
    

    The implementation of that method should load the passed user id’s information from the database, look at what type it is (partner, moderator, etc) and then instantiate the appropriate class and hydrate the appropriate information.

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

Sidebar

Related Questions

Searching a Python dictionary based on the value first, to get a key output
Sorry for the bad title, and the somewhat different questions! I have been searching
Searching around I haven't found any recent answers to this question. The other answers
Searching for values that uses the same value Example SELECT Name, UnitPrice, Quantity, Color
Searching here I found that this question was already asked , but I think
Searching for possible ways to get cookie with httpOnly enabled, I cannot find any.
Searching with '[Delphi] source control' didn't return much, so here goes: For those of
Searching from google.com, like www.abc.com Search Result Rank the pages like Title..... Description... www.abc.com
Searching online, I have found the following routine for calculating the sign of a
searching around trying to find a way to add index on suffix of a

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.