Generic class

The generic class concept, in oriented object programming, helps defining common ground for all objects that will be create from it.

The WBobject class, which documentation is available through this URL:
http://www.lapinbleu.ch/phpdocumentor/WorkBench/WBobject.html
is a perfect example of the concept in PHP.

We are going to detail how the class works to understand the usefulness and benefits of this way of programming.

Class definition

The class is defined that way:

abstract class WBobject {

// Class code

}
  • The reserved word class says that this is a class creation.
  • WBobject defines the name of our generic class.
  • The reserved word abstract is used to create an abstract class. (see explanation below)

Why an abstract class?

Before explaining why we choose to use an abstract class, it is good to remind what is an abstract class.

An abstract class is a class which won't be directly instancied. That way of casting the object is though impossible

$foovar = new WBobject();

// Exception error

This class should be used only as a parent class using the reserved word extends, this avoids the misuse of this class. In fact, to be used properly, a minimum of parameters must be defined.

Variables definition

Because we only define the common points of all objects that will use the main class, no variable has been defined.

You can freely add object common variables in this class, but keep in mind that ALL those variables MUST apply ALL children class.

Functions definition

The generic class use helps you to create functions that will be used by all your children objects.

It is though very important to code the functions you will need for your objects manipulations.

Here:

  • WBobject::__construct() : magical function that is called when the object is created. In this case, the function helps initializing variables (see example)
  • WBobject::addToDB() : function used to add an entry to the database
  • WBobject::deleteFromDB() : function used to delete an entry from the database through its ID
  • WBobject::getIDrowname() : function used to get the name of the row used to store the ID of the row in the database
  • WBobject::getObjects() : function used to define which are the variables that are objects
  • WBobject::getUniqueFromDB() : function used to obtain one row from the database through its ID
  • WBobject::objectToArray() : function used to transform an object in an array. This function is very helpful for converting a objects list in an xHTML table
  • WBobject::updateDB() : function used to modify a row from the database through its ID

It is important to note that all the database related functions use the MySQL() class

Class use

As mentionnened above, to use the generic class WBobject(), it is necessary to create a "child" class using the reserved word extends.

Code like this:

class fooChild extends WBobject {

// Class code

}

In our case, we will need to define the following variables so the class can be functional:

  • fooChild::aObjects : array containing the variable names that are objects as a key and object type as value. (see an example)
  • fooChild::database : database where the data are stored
  • fooChild::table : table where data are stored

Other variables and functions may be defined to fit the needs of the object.

The GEOcity() class can help to better understand the concept.