brendanmetzger / diatom

A template framework
1 stars 0 forks source link

Abstract Configurable Class #55

Closed brendanmetzger closed 3 years ago

brendanmetzger commented 3 years ago

Certain types of classes always crop up where they are heavily reliant on some arbitrary configuration—API keys, file paths, etc. It's always been my default to pass in a configuration constant and let the class needing it to suss out the particulars. This is not bad, as it keeps things clear enough, but passing in variables over and over is also a bit cumbersome, and muddies up the intention when scanning code.

Models, for instance, are often reliant on something arbitrary, so if the model class inherited configurable...


Abstract class Model extends Configurable { ...

where configurable is something like below, and the config file has key mappings to controller names. There are slews of philosophical backtracking to consider, but I want to pen it now to keep it on the table for pondering.


class Configurable {
  private static $keys;
  final public function config($key) {
    static::$keys ??= parse_ini_file('config');
    return static::$keys[__CLASS__][$key];
}

The other elements of having a class as a configuration data type, is the ability to do programatic configurations[1], or maybe configurations. It would probably need to be a singleton.


[1] Like defining a dynamic cache key based on some change in program state, or performing computations in RPN

brendanmetzger commented 3 years ago

Also a could be a trait