PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Three-Tier Development with PHP 5

by Luis Yordano Cruz

This article will demonstrate the power of three-tier development in PHP 5, using PEAR::DB_DataObject for the business logic and Smarty for display logic. I assume that you have some familiarity with HTML, Smarty, PEAR::DB_DataObject, MySQL, and PHP 5. If you'd like to brush up on your knowledge, the following articles explain some of the theory:

You should have installed and configured Apache, MySQL, and PHP 5 (or IIS, MySQL 5, and PHP), as well as PEAR.


DB_DataObject is an abstraction API for database access. It's a SQL builder and data modeling layer built on top of PEAR::DB. It maps database tables to PHP classes and provides easy access to common SQL functions like SELECT, INSERT, UPDATE, and DELETE. This allows developers with weak knowledge of SQL to write database-aware code and encourages a clean distinction between presentation and business logic.

(DB_OO has moved to into PEAR and is now DB_DataObject. If you have old code to update, see notes on converting old db_oo code to DB_DataObjects).

Related Reading

Upgrading to PHP 5
By Adam Trachtenberg

DataObject performs two tasks. First, it builds SQL statements based on the object's variables and the builder methods. Second, it acts as a datastore for a table row. There's a core class, which you extend for each of your tables so that you put the data logic inside the data classes. There's also an included Generator to make your configuration files and your base classes.

DataObject greatly simplifies code that accesses databases, and it makes developing large, data-driven sites easier.

At present, Alan Knowles, the lead developer of PEAR::DB_DataObject, is working on a new project called DBDO, a C implementation of the PEAR package DB_DataObjects, based on libgda. His goal is to create the next generation of PEAR::DB_DataObjects.


Smarty is template engine for PHP intended to separate the content from the presentation in a web page. It uses the GPL license.

Large projects commonly separate the role of the graphic designer from that of the programmer. However, programming in PHP has the tendency to combine those two roles in a person and inside the code. This can bring difficulties when it comes time to change some part of the page's design. If the page mixes content and presentation, the developer has to crawl through the program to find the presentation. Smarty helps to solve this problem.

Combining the Two

The first thing to do when starting this project is to create a workspace in which to store the project's code. Then it's time to configure PEAR::DB_DataObject to connect to the MySQL database MySQL (name: example), map the database tables to PHP classes, and then configure Smarty for the presentation tier for the user. Here are those steps in more detail:

Creating the workspace

Create a folder or directory called dataobjects. That was easy.

Installing PEAR::DB_DataObject

From the command line, type:

>pear install Date
>pear install DB_DataObject
>pear list

Archive_Tar       1.2      stable
Console_Getopt    1.2      stable 
DB                1.6.5    stable
DB_DataObject     1.7.1    stable  *(Goal)
Date              1.4.3    stable
Mail              1.1.3    stable
Net_SMTP          1.2.6    stable
Net_Socket        1.0.2    stable
PEAR              1.3.1    stable
PHPUnit           1.0.1    stable
XML_Parser        1.2.0    stable
XML_RPC           1.1.0    stable

Installing and configuring Smarty

Start by downloading Smarty. (I used version 2.6.5.) Extract Smarty into its own directory. From its libs directory, move the Smarty.class.php, Smarty_Compiler.class.php, Config_File.class.php, and debug.tpl files to the dataobjects directory in your new workspace.

Also move the core and plugins directories with all of their contents. Create new directories named templates, templates_c, configs, and cache.

The resulting dataobjects directory should contain:

|---- cache
|---- configs
|---- core
|---- plugins
|---- templates
|---- templates_c

11/10/2004  11:17 a.m.    <DIR> .
11/10/2004  11:17 a.m.    <DIR> ..
11/10/2004  11:17 a.m.    <DIR> cache
11/10/2004  11:17 a.m.    <DIR> configs
11/10/2004  11:17 a.m.    <DIR> core
11/10/2004  11:17 a.m.    <DIR> plugins
11/10/2004  11:17 a.m.    <DIR> templates
11/10/2004  11:17 a.m.    <DIR> templates_c
07/09/2004  09:48 a.m.  13,105 Config_File.class.php
16/04/2004  03:03 a.m.  5,117 debug.tpl
10/09/2004  02:15 p.m.  65,350 Smarty.class.php
10/09/2004  07:14 p.m.  90,924 Smarty_Compiler.class.php
              4 archivos        174,496 bytes 
              8 dirs   6,699,454,464 bytes libres

Creating the database

Create a new MySQL database named example. It will contain a table named user. Don't worry about the schema yet; that will come later.

Configuring PEAR::DB_DataObject

To build the data objects, create the following files.

require_once 'DB/DataObject.php';
$config = parse_ini_file('example.ini',TRUE);

foreach($config as $class=>$values) {
    $options = &PEAR::getStaticProperty($class,'options');
    $options = $values;

This script creates a connection to the database based on the values in the configuration file example.ini, shown next.

database         = mysql://root:@localhost/example
schema_location  = /dataobjects/schema/
class_location   = /dataobjects/
require_prefix   = /dataobjects/
class_prefix     = DataObjects_
extends_location = DB/DataObject.php
extends          = DB_DataObject

Autobuilding a database schema

The plan consists of building an object-relational mapping of the database, and automatically creating a class from the user table in the example database. All of the field names found in the table will become member variables of the class.

To create the appropriate schema:

C:\PHP\PEAR\DB\DataObject>C:\PHP\php.exe createTables.php \

This will generate User.php:

 * Table Definition for user
require_once 'DB/DataObject.php';

class DataObjects_User extends DB_DataObject 

    /* the code below is auto generated do not remove the above tag */
    var $__table = 'user';       // table name
    var $user_Id;                // int(11)  not_null primary_key auto_increment
    var $first_Name;             // string(30)  not_null
    var $last_Name;              // string(40)  not_null
    var $email;                  // string(100)  not_null

    /* Static get */
    function staticGet($k,$v=NULL) {
		return DB_DataObject::staticGet('DataObjects_User',$k,$v);

    /* the code above is auto generated do not remove the tag below */

It will also generate example.ini code for the user table schema:

user_Id    = 129
first_Name = 130
last_Name  = 130
email      = 130

user_Id = N

Pages: 1, 2

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: