Assume I have an object with 3 properties:
protected $validMainStatements;
protected $validPrimaryStatements;
protected $validSecondaryStatements;
And I got the following method:
public function selectType($stmt) {
$stmtParts = MainServerInterface::parse($stmt);
$type = $stmtParts[0] //returns either Main, Primary or Secondary
}
Depending on the value of type, I want to use the associated property. A simple implementation would be:
public function selectType($stmt) {
$stmtParts = MainServerInterface::parse($stmt);
$type = $stmtParts[0] //returns either Main, Primary or Secondary
if($type === "Main") {
$usedProp = $this->validMainStatements;
} elseif($type === "Primary") {
$usedProp = $this->validPrimaryStatements;
} elseif($type === "Secondary") {
$usedProp = $this->validSecondaryStatements;
}
}
I think I don't have to mention that this is ugly and uncomfortable to use. Is there a way to implement this in an easier way? Something like (pseudocode):
$usedProp = $"valid".$type."Statements";