I am currently refactoring an application into multiple classes in an attempt to fulfill Single Responsibility Principle; however, many methods in the original mammoth classes use one common “metadata” object (bound as a class property) for their business logic.
For example:
if($this->metadata->applyTracking) {
// perform tracking logic
}
When I am separating these classes out, I am considering two options:
- Passing this object to particular methods of the class, on case by case basis (Can be many occurences).
- Adding this object as class properties (Many classes will have this property injected).
- Making the object as a Singleton (I am wary of this approach since it may share the same fallbacks as globals)
Any advice on which path to take?
Method #2 seems to be best. I would inject an object repository, in which each member of the repository would be a different object that provides a different service. An example class can be found here: https://github.com/kenaniah/insight/blob/master/classes/registry.php