I've written an OOP web interface to update and display various database records. There is a single "Record" class that contains 95% of the code to initialize, edit, add, update & display the records. For each kind of record there is a small additional "ThisRecord" or "ThatRecord" class that extends Record and contains a data structure defining the record fields, plus a customized public display function for that record. (There are also some other classes for DB interface etc.)
The bulk of the code is used only for editing the data, which is done seldom compared to the frequent accessing of the data for display. So I'd like to conditionally include the editing code only when it is needed. I tried the following tactics, neither of which worked:
Move all of the edit-only code into a separate file "editcode.php", create a global $editMode variable, and in Record class:
if ($editMode) require "editcode.php";
The parser didn't like having an if statement within a class but not within a function.
Create a Record_Edit class that extends Record class and includes only the editing functions. Then define a class name variable:
if ($editMode) $recordClass='Record_Edit' else $recordClass='Record';
and define the specific classes to extend this variable class name:
Class ThisRecord extends $recordClass
Predictably, the parser doesn't like having a variable class name.
I can't see another way to bash this thing into working without creating two separate modules that partially duplicate each other's code. I am hoping that one of you might be able to see something I did wrong in my two approaches, or suggest another (at least reasonably clean) alternative.
Thanks in advance for your thoughts! 🙂
Donna