PHPOF v1 Tutorial: Creating the Common File

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

Before we start coding properly, now is a good time to bring together the central requires that we are going to need globally across the whole site and create a common.php file containing all these.

First, we need to sort out a database connection. As mentioned before, there's little point in repeating this across all pages, so we centralise it in a common file and use a single global variable with a fixed name ($db) across all pages. We call DB::connect on an uninitialised DB object, which will return an initialised object of type DB_<dbtype> (e.g. DB_mysql).

require_once('DB-phpof.php');

// Connect to the database
$db = DB::connect("mysql://test:test@localhost/phpof_test");

We now include the rest of the PHPOF modules which we're going to use (you will see more of these later):

require_once('DBTable.class.php');
require_once('DBRow.class.php');
require_once('XArray.class.php');
require_once('XError.class.php');
require_once('XString.class.php');
require_once('Token.class.php');

We of course need to include the file containing the derived classes we created earlier:

// Our own derived products and product classes
require_once('products.class.php');

And finally, we set up a global associative array which contains definitions of our product categories, using a simple identifier as the key (which will be used in the database column 'category') and a more verbose description as the value.

$product_categories = array(
'hardware'=>'Hardware and fixings',
'food'=>'Food and drink',
'pets'=>'Pet supplies'
);

< Prev: Creating the Derived Classes