2

Imagine I've got a class defining an entity, such as a Product.

My framework is structured in toggable modules, such that some modules may extend the default class, i.e:

  • Module1 lets me define a ColoredProduct
  • Module2 lets me define a ShapedProduct

How would I define the relations between ColoredProduct, ShapedProduct and Product? How can I make sure that I can get a dynamic relation (toggle one or another without code intervention)?

Do I extend the class product? Do I refer to it on each subclass?

I'm using PHP. Thanks for your input.

repptilia
  • 457
  • 8
  • 19
  • what does mean "toggle one or another without code intervention" phrase in your case ? – RomanPerekhrest Mar 04 '16 at 20:51
  • That means that I can enable or disable my modules. – repptilia Mar 04 '16 at 20:52
  • can you show how would you primarily enable/disable one of those modules technically ? – RomanPerekhrest Mar 04 '16 at 20:55
  • Technically, I have a database table with a list of enabled modules. My interface displays my modules either enabled or not depending on this table, and this presence on my modules folder. Adding a module dynamically modifies a composer autoloader which then loads my classes (`$classLoader->addPsr4...`) – repptilia Mar 04 '16 at 20:59
  • so, where have found such approach? Every time your code is running and initiates database query to verify whether the current module enabled or disabled. What is the benefits ? – RomanPerekhrest Mar 04 '16 at 21:15
  • Well this is cached. The benefit is to have an admin user configure his platform depending on requirements without code interventions. This becomes especially handy since I can host multiple applications on a same instance. – repptilia Mar 05 '16 at 04:08

1 Answers1

0

This sounds like you are looking for a class hierarchy:

  • Product is the superclass
  • ColoredProduct and ShapedProduct are subclasses.

You need no additional code since the subclasses inherit methods and properties from the Product superclass.

Depending on how you implement modules this could also be solved using Interfaces or traits.

If your question is more about designing application modules that are related this may be of interest: what is the difference between loose coupling and tight coupling in object oriented paradigm?.

For a very good example of how independend modules can be designed in PHP you have to look at the Modules in Yii2 Framework.

Community
  • 1
  • 1
WeSee
  • 3,158
  • 2
  • 30
  • 58
  • My only issue with subclasses is that I'm not sure how subclasses would interact with each other in the generic scope. How can I benefit from the two behaviors at the same time? Anyway, maybe this will enforce putting my application logic to the correct place. Thanks – repptilia Mar 05 '16 at 04:11
  • To let the subclasses interact with each other define interaction methods in the Product superclass. Or use events to communicate between subclass objects which send and receive events. The event pattern supports loose coupling of objects instead of direct invocation of methods. Again Yii2 delivers some good tools for that purpose (despite the framework as a wohle has to be used). – WeSee Mar 05 '16 at 08:01