Jump to content

Polymorphic User Account Classes


fry2010

Recommended Posts

I have been reading a lot on polymophism and understand the basics, however when it comes to implementation on something as diverse as different user account types, I struggle to make sense on how best to achieve it. So wondered if someone could give me some pointers as to achieve it:

 

My database tables:

 

user_account (

id mediumint(7)...

firstName varchar(120)...

lastName varchar(120)...

accountTypeId tinyint(1)...

etc...

)

 

account_type_1_profile (

userId mediumint(7)...

getEmails tinyint(1)...

aboutMe...

showDemo...

)

 

account_type_2_profile (

userId mediumint(7)...

differentProfileSetting1 tinyint(1)...

differentProfileSetting2 tinyint(2)...

)

 

As you can see I have a single table for all user account types, but different profile tables. I felt this was best as different accounts will have different profile information. Also I will be having user groups into the mix, so again create another table for group profile settings and a user to group table.

 

Now when creating accounts I have to create a main account, and also a profile for the user depending on the type of user.

 

My current system is:

 

class userClass()

class userAccountClass() - create and edit the account table

class userProfileClass() - create and edit the user profile

 

Within userAccountClass() I have a method called createAccount() - which creates any type of account.

within userProfileClass() I have a method called createProfile() - which is where my issues start to arrive. How can I build it to be polymorphic given that there are a variety of different account profile types?

 

How do I avoid using a big switch statement or multiple methods to achieve same result?

 

Suggestions welcome..

 

Link to comment
Share on other sites

Just in case anyone wondered, I eventually got a fairly good polymorphic design setup (at least I think so):

 

Created a base class called 'profileObject'. This had a static method called 'getProfileObject(userObject $userObject)'.

 

As you can see this accepted an object of type userObject. This method auto-loaded the required sub-class.

For example if the userObject account type was an affiliate, then it would load the sub-class called 'affiliateProfileObject()'.

 

The sub-class extended an abstract class called 'Profile'. This obviously set out the required methods and properties.

 

Now anytime I want to get profile of a user object I simply call:

 

$userProfileObject = profileObject::getProfileObject($userObject);

 

Then if I need to access a property of that users profile I can simply use

 

$userProfileObject->avatar;

 

Hope this helps someone else. Of course if anyone wants to make suggestions on how this could be improved etc I would be interested to hear it. cheers.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.