Saturday, March 18, 2017

Architecture recommendation for large class?

I have a class right now with the following shell:

class Chapter {
    ...

    public function chapter_001(){}
    public function chapter_002(){}
    public function chapter_003(){}
    ...
}

Ordinarily, the method would just be chapter (without the _xxx) and a parameter to know which to query for, but in this case each method runs quite a bit of code that varies the output based on the spec, each independent from the last save for a couple private methods and a configuration object in the constructor.

Worse yet, I wind up calling the right method via some not so tasty code:

if (method_exists($chapter, "chapter_" . $id)) {
    // Do things
}

Is there a better way to structure something like this?



via Chords

Advertisement