The topic of dependency injection is one that’s been around for some time in object-oriented programming circles. Sometimes we see it in WordPress; sometimes we don’t.
I’m a fan of it but, honestly, though, I’m not always sure how much information to inject into a class. I mean, let’s say that we’re given two classes, and one holds information the other needs.
- Do we inject no class into another class?
- Do we inject only a piece of information (be it a string, integer, data structure, or whatever) into the other class?
I don’t think there’s a hard and fast rule for this, but it’s probably safe to say that it’s better to inject just the data you need. But then this raises a question of how do prepare the data to inject into a given class?
- Do you create a method in one class and pass it into another?
- Do you pass a piece of private or protected information into it?
Then again, I think it depends on if anything has to happen to the information before it’s passed into a class.
Anyway, I could go back and forth on this for the rest of the post and never come to a conclusion so why not work through some source code until there’s something reasonable.