I have a configurable report. For each field that can be included on the report, there's a key (stored in report preferences), a label, potentially an access level, and a SQL descriptor -- something like foo as my_foo
.
In a Java app, I would create a class called ReportField with each of the properties listed above. I'd use a private constructor, and list each of the fields in the class like this:
public final static ReportField FOO = new ReportField('foo', 'Foo', 1, 'foo as my_foo');
I'd probably create a static array of all of the fields, add a static method that allows a field to be looked up by key, and so forth. Then in other places I could write code like:
List<String> selectFields = new ArrayList<String>();
for (ReportPref pref : reportPrefs) {
selectFields.add(ReportField.getByKey(pref.getField()).getFieldSql());
}
Apologies for the Java code, but hopefully you get my point.
Is there an idiomatic way to solve the same problem in PHP? I can think of a number of solutions -- nested associative arrays will do the trick -- but I'd like to avoid a hackish solution.