NotionCommotion Posted October 27, 2021 Share Posted October 27, 2021 Many will say "favor composition over inheritance". Others will say "Use inheritance when you want to add functionality and use composition when you want to modify functionality. Both seem like sound advice. My current predicament doesn't completely fit but hoping it does enough. Working on a multitenant application where most entities must belong to a given Tenant, so I create the AbstractEntityBelongsToTenant class who's sole purpose is to add the Tenant to the entities and extend all the other classes from it. I then have listeners which look for AbstractEntityBelongsToTenant and filter, enforce, etc. Turns out I have two types of users: TenantUser which belongs to a given tenant and VendorUser which belongs to a given Vendor which in turn belongs to a given Tenant. So, I create AbstractUser which has properties such as name, email, etc, make it extend AbstractEntityBelongsToTenant, and make both TenantUser and VendorUser extend AbstractUser. All good! But then I realize I need a SuperUser which also has properties such as name, email, etc, but doesn't belong to a single Tenant. Hummm... When inheritance doesn't diverge, I think it is simple and easy to understand, and while this scenario isn't real common, it more common than I would like to admit. I am thinking for this case to create EntityBelongsToTenantTrait and make TenantUser and VendorUser use it, and then make both those classes implement EntityBelongsToTenantInterface. Suppose I could make UserTrait but don't think it is as obvious. I am not saying one should favor traits with interfaces over inheritance all the time, but am asking whether one shouldn't hesitate to use traits with interfaces when this issue comes up and composition doesn't solve the issue and if there are any/many negative implications of doing so which I might be unaware of. Thanks Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.