Ignoring the fragility, performance and even architectural arguments associated with superclasses (I like to just put my fingers in my ears, repeat ‘na na na’ over and over, and pretend these problems don’t exist), I’m fond of declaring a single base class that can be extended by almost every other class in an application. (For the sake of this question, I’m thinking of PHP4, so will ignore the loveliness of interfaces, and other functionality beyond my means.)
So, if you were to implement such a ubiquitous base class, what methods would you put it in? To begin with, I’d consider the following:
- A save or persist method, which could record the current object data to a configurable location: database, disk, memory (e.g. memCache), or session (cookie). This would also require a set of methods that define the mapping of properties to database tables/fields, and relations between classes (e.g. compositions).
- Conversely, a load method, based on a unique identifier or condition.
- A getAsXml() method that returns an XML representation of the object (either as a string, or appended to a provided DOM Node). Actually, maybe this should just be an ultra-flexible export or getRepresentation type method, that can also return the object as HTML, CSV and other formats.
What else might be useful, for debugging, logging, portability or otherwise? What about useful abstract methods, maybe for comparing or merging?