Using a hierarchy of classes, where subclasses have some unique features

malcomut2003

New Member
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;\[code\]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);}\[/code\]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:\[code\]class NumberCell { public function render(TableCell $cell) { $cell->setContent("123.45"); // if this is a HTMLTableCell $cell->setClass("number"); }}\[/code\]What's the best way to handle this? Checking the type at runtime seems kind of hacky. I'm open to changing the design.
 
Back
Top