Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using PHP Objects to Access Your Database Tables (Part 2)

By Tony Marston
2005-04-07


Virtual Database Tables

By this I mean creating a class for a database table where some of the apparent details of the class, when accessed by the calling script, may exist in a different form on the physical database. Because all the details about each database table are encapsulated in the class for that table, and because all communication between a PHP script and the physical database are routed through the class, it is possible to make subtle changes between the calling script's view of the database and what physically exists.

This may sound very esoteric, a typical case of 'very clever, but what use is it?', but there may be a time when this ability comes in very useful. Here are some examples:

You have coded a class for a database table, you have coded all the scripts which communicate with this class when you suddenly find that your RDBMS will not let you create a table with that name because it is a reserved word. Instead of having to change lots of lines of code in lots of scripts all you need do is go to the class constructor for that table and change

   $this->tablename = 'reserved_word';

to
   $this->tablename = 'not_a_reserved_word';


So as far as all your PHP scripts are concerned they are talking to a database table called reserved_word when in actual fact the database object is transparently redirecting them to not_a_reserved_word. You may come across other legitimate reasons for having to rename tables, or move them from one database to another, but by using classes in this way it is possible to redirect your whole system simply by changing one single line of code.

It is possible to create alias names for databases tables. This is useful if you are using different rows of the same database table for different purposes, such as in a senior-to-junior relationship, and you need to identify which is which. Creating an alias name is as simple as this:

<?php

require_once 'real.class.inc';
class Alias extends Real
{
} // end class
?>


Because your PHP script instantiates an object from the Alias class it thinks it is talking to a database table called Alias when in fact it is really talking to a table called Real.

As well as giving database tables different names it is also possible to rename individual columns. As all the data communication between the PHP script and the object is via standard arrays and not database resources it is therefore eminently possible to intercept each communication and use standard PHP array processing functions to change a column name from this to that when going one way, and from that to this when going the other.

It may become necessary in the lifetime of a system, for reasons of performance for example, to split a database table into several parts. This situation could be dealt with simply by changing the class dealing with that table. All the scripts using the class would still think of it as a single database table, but only the class itself would really know that it is actually several tables.

Tutorial Pages:
» Intended Audience
» Extending the MySQL SELECT statement
» Adding the features of a Data Dictionary/Repository
» Using the Data Dictionary/Repository
» Changing Candidate Keys
» Deleting Rows
» Custom Processing
» Virtual Database Tables
» Summary


 | Bookmark
Related Tutorials:
» Zend Framework Tutorial
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1