mardi 24 janvier 2017

Adding and abstract subclass in PHP without modifying all the inheriting children classes

I have a similar class hierarchy

interface ISomething
{
    public function foo();
    ...
}

abstract class TopAbstract implements ISomething
{
    ...
}

abstract class HighAbstract extends TopAbstract 
{
    public function foo()
    {
        ...
    }
}

and 46(!) classes that inherit from HighAbstract 

class One extends HighAbstract
{
}

...

class FortySix extends HighAbstract
{
}

Now I have to add functionnality and that requires having an alternate HighAbstract::foo() (amongst other things). What would be the best way to achieve that?

  • I could just modify HighAbstract but I'd like to keep that new functionality separate (separation of concern)

  • I could add a subclass to HighAbstract but then I would have to edit the 46 subclasses to change their parent.

  • I could add a trait to HighAbstract but that still implies modifying it (HighAbstract) with branching and using the trait in HighAbstract::foo()

  • Any other ideas?

I think using the trait with minimal edits in HighAbstract may be the least bad solution, that way the code is somewhat separated and if the new functionality is ever dropped, it could be done with minimal changes.

What do you think?

Thanks

Aucun commentaire:

Enregistrer un commentaire