fry2010 Posted August 30, 2012 Share Posted August 30, 2012 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 https://forums.phpfreaks.com/topic/267791-polymorphic-user-account-classes/ Share on other sites More sharing options...
fry2010 Posted September 3, 2012 Author Share Posted September 3, 2012 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 https://forums.phpfreaks.com/topic/267791-polymorphic-user-account-classes/#findComment-1374828 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.