I am coding a php script that extracts data from json files and maps/saves it to appropriate structures in a DB. There will be several slight variations based on the content of the file. Now I was wondering which option is best suited for the code.
Assumptions: The code is not used as a component in other code The classes are not instantiated, but rather just a collection of static methods.
- Put all methods in a single class:
class Extractor {
public static function readFile() { }
public static function saveToDB() { }
public static function mapResumeData() { }
public static function mapEducationData() { }
}
- Have a baseclass Extractor and individual Childclasses based on what to extract:
class Extractor {
public static function readFile() { ... }
public static function saveToDB() { ... }
}
class ResumeExtractor extends Extractor {
public static function mapData() { ... }
}
class EductionExtractor extends Extractor {
public static function mapData() { ... }
}
- Use an interface as well (should I always do this? Best practice?):
interface IExtractor {
public static function readFile();
public static function saveToDB();
public static function mapData();
}
class Extractor {
public static function readFile() { ... }
public static function saveToDB() { ... }
}
class ResumeExtractor extends Extractor implements IExtractor {
public static function mapData() { ... }
}
class EductionExtractor extends Extractor implements IExtractor {
public static function mapData() { ... }
}
- Don't use classes and just put all functions in a namespace
use namespace Extractor;
function readFile();
function saveToDB();
function mapData();
- use traits