PHPOF v1 Tutorial: Creating the Derived Classes

This page is about the original version of PHPOF (version 1). This has been superceded by the completely-rewritten PHPOF version 2.

The key PHPOF classes

PHPOF contains a number of classes, but the key ones are the database classes (DB, DB_type, DBTable and DBRow). We'll skip the DB and DB_type classes for now, since DBTable and DBRow are the ones you'll have most interaction with.

The basic idea is that these two key classes model the database structure. An initialised DBTable object will contain information about the table it represents, and will allow you to easily perform certain functions (such as searching for rows) at a table level. Similarly, a DBRow object holds the data stored within a particular row and will allow you to fetch, insert, update and delete rows with ease.

In a very simple application, you could use the DBTable and DBRow classes directly, but it is strongly recommended that you create derived classes rather than use the base classes. The chances are that somewhere down the line you'll want to add your own member functions and if you do, you'll then have to go through all your code and change the constructors to use your derived class instead. Much better just to start off with derived classes, plus it means you can make things simpler for yourself as you will see later. Trust me, you really do want to use derived classes.

Creating the products class

OK, let's get started. The first thing we need to do is create a derived class 'products' based on DBTable which will model the 'products' table we set up earlier. I'll take you through the steps of creating the file products.class.php. First, we need to set up the class and create a constructor. We're essentially going to throw the object construction request back up to the base class but we'll substitute in the required parameters.

DBTable requires an initialised database object and a table name. To make things simple, we'll assume we've already got a global database object $db set up (we'll cover this later). We know the table name, so we can create a constructor that requires no parameters but which will return an initialised 'products' object like so:

class products extends DBTable{

  function products () {
    global $db;
    return $this->DBTable($db, 'products');
  }

We're going to allow the products in our table to be categorised. This will give us an opportunity to demonstrate a number of things, including using member functions. As an arbitrary example, let's say we want a member function that will pick out all food products. We'll assume that our product category for foods is going to be called 'foods' (will be defined in common.php later).

  function GetAllFoods () {
    return $this->BasicSearch('category','food');
  }

}

Here you can see we're using one of the standard functions we've inherited from the parent class - DBTable::BasicSearch(). The above function call will essentially result in an SQL query something like "SELECT * FROM products WHERE category='food'". There are similar functions such as DBTable::BasicSearchLike() and DBTable::BasicSearchNot() for doing other simple searches.

That about wraps up our DBTable-derived class for now. Not too painful?

Creating the product class

Now we need to create the class derived from DBRow, which we'll call 'product'. We'll keep it in the same file as the 'products' class, for simplicity.

The process is again similar; create a constructor which throws the request up to the parent class's constructor but eliminating any superfluous input parameters (none in this case), and then create any custom member functions that are required. For now, we'll leave it without any member classes. (As discussed earlier, this means we could just use the base class, but creating a derived class leaves the route clear for future expansion).

We need to pass DBRow an initialised DBTable object, plus (optionally) the key of a row which we would like to be fetched into the row object upon construction.

class product extends DBRow {

  function product (&$products, $key = '') {
    return $this->DBRow($products, $key);
  }
}

< Prev: Introduction - Next: Creating the common file >