Page 1 of 1

What makes a good base class

Posted: Fri Mar 03, 2006 3:52 pm
by alex.barylski
I have been working towards a framework collection of classes, similar to that of say C++ frameworks (wxWindows, OWL, MFC, etc).

Obviously my framework is quite different, cuz there are no CListCtrl controls or widgets...all that is done via template engine and HTML.

As you may or may not know, the above frameworks all have many classes (some supporting like CString and other more fundemental classes like CWinApp) each of which usually derives from another and most if not all derive from one single common base class (CObject in MFC).

The purpose of CObject is to provide a generic base class which all classes can benefit from...in this case (MFC) it provides some basic debugging, RTTI, etc...

To emulate this class in PHP is almost non-sense, but I figured I would anyways...

This is the base class API I have come up with:

Code: Select all

class CBase{
  dumpData(); // Dump all object properties when called - all child class props too??
  assertValid(); // Pure virtual: No implementation in base class - override in children
  serializeObject(); // (un)Serialize all child classes data
  logError(); // Basic error logging
}
RTTI is a basic functionality of most scripting languages, so it's pointless to try and cover that in a PHP base class. As well as error logging, etc...but I still like the idea of having a generic base class from which to derive all other classes...

dumpData() I would like to implement in the base class only and have it be smart enough to determine it's child classes when instantiated. If this can be done, then I would like to extract all child class properties, etc and dump them to stdin.

I understand that this function is wasteful in PHP because there are no preprocessor directives to remove it from release mode, but still...it's overhead should be small enough not worry about it.

assertValid() in this base class serves no purpose but to define an API...it's a pure virtual function...it doesn't get implemented in base class. In derived classes though, it is used to test the value of data members, etc...

Again wasteful in PHP because of the lack of PP directives and it kind of does what unit testing is designed to do, I've become quite accustomed to this approach in C++ and want it in my PHP development as well :)

serializeObject() will (un)serialize all derived classes properties using either sessions or files or something...unclear as to the details for this yet :)

logError() is basically just going to allow easy logging of errors, alongside with TIME, IP, etc...unclear on details for this yet too :)

So understanding where I'm coming from and where I wanna go, I'd like to hear your input on having a base class which provides basic generic class functionality...

Ideas, suggestions, comments, etc...

Cheers :)

Posted: Fri Mar 03, 2006 5:20 pm
by Christopher
I'm not a fan of base classes because I have rarely found that large groups of classes need common functionality. Interfaces are a middle ground even if not actually implemented, but that's more for polymorphicism -- which is one of the main things you are trying to achieve. Three of your four methods are for debugging and a better direction would be unit testrather than implementing those. That leaves serializeObject() which is not that frequently needed and might be better to have a Serializable class/interface for the cases where you need it.