I want to create an object, but depending on the situation, I might want to populate it based on a single parameter, or else based on two totally different parameters. As I have some time to do this "right", I'm wondering what might be considered the best practice(s) for this situation. One approach I'm not too crazy about:
public function __construct(PDO $pdo, $id=null, $arg1=null, $arg2=null)
{
if(!empty($id)) {
// populate $this by ID
}
elseif(!empty($arg1) and !empty($arg2)) {
// populate $this by those 2 args
}
else {
// probably throw an exception?
}
}
Another approach, also one I'm not crazy about, is providing 2 different "populateBySomething()" methods you would call after first creating the (empty) object.
And then there are random neuron firings with vague thoughts about using some sort of Factory for this? Or make them into different classes extending an abstract class or implementing an interface?
I'm thinking the "best" solution is probably the one I haven't thought of yet in my current state of sleep deprivation.