dimanche 13 décembre 2015

How to Create Co-Dependent Objects in PHP

What are the options/patterns that I should be thinking about when I am creating co-dependent objects? (I'm using PHP). For example:

  • I have an Organisation object that has a required Email property
  • Oganisation is also a required property of Email

I am creating all objects in factories, so I have a fair degree of flexibility. To date I have been relaxing the constraint on one of the objects, and giving it a setter that works once only. For example:

  • On the Email object I would make Organisation an optional property
  • I would then add an email->setOrganisation($org) method, which would check that the value of Email::Organisation was null before going ahead and setting it.

But is there a better way? One option I have thought about is creating a prototype object (in this case Email), and allowing prototypes only to be replaced.

Aucun commentaire:

Enregistrer un commentaire