Method inheritance
Posted: Sat Dec 22, 2007 1:07 pm
I can't seem to create an interface that allows me to specify a class type in the method, then specify a subclass type in the implemented version of the method. I'm trying to find a way to do this without having to manually check the type inside of the implemented method. Here's an example of what I'm after:
So far, the best I can think of is:
Code: Select all
class Item_Abstract
{
// ...
}
class SpecificItem extends Item_Abstract
{
// ...
}
interface TestInterface
{
public function add(Item_Abstract $item);
}
class SpecificAdder implements TestInterface
{
public function add(SpecificItem $item)
{
// ...
}
}
$adder = new SpecificAdder;
$adder->add(new SpecificItem);Code: Select all
class SpecificAdder implements TestInterface
{
public function add(Item_Abstract $item)
{
if ($item instanceof SpecificItem) {
// ...
}
}
}