PHPOF v1 Basic Concepts

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

Introduction

PHPOF is divided into two clear areas:

The utility functions/classes are relatively simple and self explanatory - just have a look at the reference documentation to see what's available. Here, we'll focus on using PHPOF's database functionality, which is what it was primarily developed for.

Table/Row concepts

PHPOF uses a concept somewhat different to other database classes: it logically separates the roles of database tables and rows. The two 'core' classes contained within PHPOF are 'DBTable' and 'DBRow', which are accompanied by the 'DB_result' class to handle the transient results of queries. Contained within these are generic functions for manipulating tables, rows and query results. As you'd expect, the 'table' class contains things to do with tables generally (primarily searching the table in various ways), and the 'row' class deals with actions you'd want to do with individual rows (insert, update, delete etc.)

One of the main features of PHPOF, that makes it stand out from other similar classes, is it's automatic table detection. What this means is that when you instantiate a DBTable object for a particular table in your database, PHPOF automatically determines the columns that are in that table. This makes it very powerful for rapid application development, because you don't need to craft big SQL queries for inserts and updates to each of the table objects in your application. PHPOF does it all for you! This is particularly important in environments, where new features often need to be added and therefore new columns are often being added to databases. With PHPOF, you don't have to track changes to your table structure with amendments to big SQL queries - it just happens automatically!

Although you can in principle instantiate DBTable and DBRow objects directly, this is not recommended. Rather, for each table in your database, you create your own table and row classes derived from DBTable and DBRow. This allows you full access to all the features of DBTable and DBRow, but allows you to add functions appropriate to your own particular application.

Table caching

To speed up automatic table detection (especially if you use PostgreSQL, since it's 'expensive' with that database), you can enable PHPOF's integrated table-caching functions, which use a DBM database to store the results of column layouts within tables.

Hooks

If you follow my suggestion of creating derived classes, you can also tap into 'hooks' (callbacks) that are provided throughout PHPOF (especially in the DBRow object, mainly at the entry and exit points of important functions). These allow you to insert a snippet of code (normally a call to another function) that will be eval()'d at the appropriate point. In most cases these are executed in the context of your object (so you can use constructs like "$this->myCallbackFunctionn();", but watch out for the DB_result::fetchIntoObject() hooks, which are executed in the context of a DB_result object.

Audit Logging

PHPOF includes integrated audit logging. It requires you set up two tables in your database, for 'events' and 'actions'. If you do this, and enable audit logging, you will find that all database changes conducted through the insert, delete and update methods provided by PHPOF will be automatically logged. Each specific instance will be called an "event", and for each event, the exact changes that were made to the database table in question will be stored as "actions".

Configuration

PHPOF is fairly configurable and has an array of options that you can set on a global, sitewide or per-page basis. Essentially, the defaults are stored in the file PHPOF-defaults.conf, which you should include in each page that utilises PHPOF (typically via a site-wide 'common' include file). You can then define any changes to the default configuration which you wish to make. For more information, please see the configuration chapter.

Tutorial

You may wish to follow the quick-start tutorial, which covers some of these concepts in a practical way.

Storing data

If you use DBRow::store(), the logic is illustrated in the diagram below:
Diagram illustrating the logic used when storing a row using DBRow::store()