Configurable PHP Classes
The MooTools Class construct lets you pass an options array to the constructor. This means you can have variable parameters — a useful feature when you don’t want to commit to a particular interface, or when you have a large number of options that may or may not be present.
The options can be assigned default values by setting an options array in the Class definition. This default options array works with inheritance, so subclasses inherit the parent class options and can also redefine defaults and add new options.
I wanted similar functionality in PHP. Solarium, one of my most heavily used libraries, comes with a “Configurable” class from which it inherits. This lacks the inheritance functionality for the default options, which is an essential feature.
After a bit of playing around with things with fancy names like “late static binding” I managed to modify the Solarium Configurable class to get something that works the right way:
Use it like this:
I’m not sure if using this sort of configuration scheme in PHP is considered good programming practice or not, but if you find that you do need configurable classes, now you have a base class to start you off.