Here's a design problem that I run into sometimes. Say I have a set of related classes, with some common features and some unique ones;
interface TableCell {
public function setContent($content);
}
interface HTMLTableCell extends TableCell {
public function setID($id);
public function setClass($class);
}
interface CSVTableCell extends TableCell {
public function setQuoted($quoted);
}
Then I have some classes that need to interact with any object from the set. It's easy to use the common methods. But I want the class to be able to use the unique methods as well, when they're available:
class NumberCell {
public function render(TableCell $cell) {
$cell->setContent("123.45");
// if this is a HTMLTableCell
$cell->setClass("number");
}
}
What's the best way to handle this? Checking the type at runtime seems kind of hacky. I'm open to changing the design.