I'm writing a database interface in PHP and I have a base dbTables class, as well as a base dbTableFields class.dbTables has a function, getFields(), that instantiates dbTableFields objects, each of which carries essential data on each table field, and puts it into dbTableFields->fields=array();dbTables has several children.Now I've realized that the children need to use different versions of getFields(). But they only differ in one place: they need to instantiate dbTableFields children, rather than dbTableFields objects.I've solved this problem. I took advantage of PHP's ability to use variables to represent class names. All I had to do was, in dbTables->getFields() replace:$this->fields = new dbTableFields();with $this->fields = new self::$field_model();Where self::$field_model = 'dbTableFields'; // string value for classNow, in the child classes, I just have to override the $field_model property with the name of the appropriate dbTableFields descendents.Is this parallel inheritance scheme good form? Or am I creating an uncompromising mess? Is there some more-elegant and more maintainable method for achieving the same effect?
Does Parallel Inheritance Make for Good Code?
php;inheritance;dynamic inheritance
The principle you want is to favor composition over inheritance.  This is a fairly classic example of where the strategy pattern would be useful: separate your getFields logic into several FieldStrategy objects, and reference the appropriate one for your parent and your children.I'm not explaining this very well, but please google strategy pattern and play with it.