I know I cannot extend from two classes, but what is the alternative for my case?
I am using a base class, parser
, that parser the page of my CMS. This class contains all the basic functions needed to filter the data retrieved from the database and rendering it into a HTML page.
All the other classes NEED the parser, because without it they don't work.
I have 2 modes:
- Inside the CMS
- Outside the CMS
Inside CMS
If inside the CMS, userdata and other additional data is loaded into the class.
Outside CMS
If outside the CMS, only the necessary data to render the page is loaded, this is the default way for displaying pages to people who visit the site.
Modules
A page can be used to display default data/elements, but it can also be used to display data from a module (e.g. a calender page). If this is the case, additional data needs to be loaded into the parser object, and thus I have 4 different use cases:
- parser mode
- cmsParser mode (inside CMS)
- moduleParser mode (parser with module data loaded)
- cmsModuleParser mode (both)
I have the following [extremely simplified] classes:
class parser {
protected $oDataSource1;
protected $oDataSource2;
protected $oDataSource3;
//...
public function filterData() {
//.. Search through the data sources and return filtered data
}
}
class cmsParser extends parser {
protected $sUser_name;
protected $iUser_id;
protected $sUserLanguage;
///.. some functions here that are called only within the CMS
}
class moduleParser extends parser {
protected $mModuleData;
//.. Do something with this moduleData;
}
class cmsModuleParser extends ?? {
//... Get functions from the cmsParser + module functions
}
The only solution I can come up with is using a trait that the moduleParser and the cmsModuleParser both use? This is not optimal IMO, because I still have to add duplicate variables etc.
I don't want duplicate code, of course, so how do I solve this puzzle?