The PEAR Class
The base PEAR class is fairly abstracted and shouldn’t be used on its own, however, it is a great class to
build your classes off of. It’s major feature is that it imitates destructors. With PHP5 on its way this will
be void, since PHP5 will support destructors natively, I believe. The class file for PEAR also includes the
PEAR_Error class, which we will talk about in more detail at a later time. First let’s discuss basing your
classes on PEAR. We are going to start by creating our own specific base class and then extending that to a
user class to be used on a site. Remember to document your classes using PHPDoc!
build your classes off of. It’s major feature is that it imitates destructors. With PHP5 on its way this will
be void, since PHP5 will support destructors natively, I believe. The class file for PEAR also includes the
PEAR_Error class, which we will talk about in more detail at a later time. First let’s discuss basing your
classes on PEAR. We are going to start by creating our own specific base class and then extending that to a
user class to be used on a site. Remember to document your classes using PHPDoc!
<?php
require_once('PEAR.php');
require_once('DB.php');
require_once('Log.php');
/**
* Default PEAR DSN
*
* @author Joe Stump <[email protected]>
* @global string BASE_PEAR_DSN
* @access public
* @see Base::Base(), Base::$db
*/
define('BASE_PEAR_DSN','mysql://root:@localhost/base');
/**
* Base Class
*
* Our base class will hold only the basic necessities that all
* of our child classes will need. Mainly DB connectivity and
* the ability to log errors.
*
* @author Joe Stump <[email protected]>
*/
class Base extends PEAR
{
/**
* DB Class
*
* @author Joe Stump <[email protected]>
* @access public
*/
var $db;
/**
* Log Class
*
* @author Joe Stump <[email protected]>
* @access public
*/
var $log;
/**
* Base Contstructor
*
* Connect to the DB and create our Log, which can then be
* used by all children classes.
*
* @author Joe Stump <[email protected]>
* @acces public
* @return void
*/
function Base()
{
$this->PEAR();
if(
get_class($this) == 'base')
{
$this = new PEAR_Error('Base is an abstracted class!');
}
else
{
$this->db =& DB::connect(BASE_PEAR_DSN,true);
if(DB::isError($this->db))
{
$this = new PEAR_Error($this->db->getMessage());
}
else
{
$this->db->setFetchMode(DB_FETCHMODE_ASSOC);
$this->log =& Log::factory('syslog','Base');
}
}
}
/**
* Base Destructor
*
* Just add a '_' to your class's name and voila! you have a PEAR
* controlled destructor!
*
* @author Joe Stump <[email protected]>
* @access public
* @return void
*/
function _Base()
{
if(!DB::isError($this->db))
{
$this->db->disconnect();
}
}
}
?>
There are a few things that you will notice that are quite a bit different from your average PHP class. The first is
it’s documented! No, just kidding, we all document our code. Jokes aside you’ll notice that all we really needed
to do to make our class a true PEAR class is extend it from PEAR and make sure we had a properly named destructor.
it’s documented! No, just kidding, we all document our code. Jokes aside you’ll notice that all we really needed
to do to make our class a true PEAR class is extend it from PEAR and make sure we had a properly named destructor.
There is a side note on destructors. When you create an instance of any PEAR based class you MUST
assign by reference, meaning =& and not just =. If you do not assign by reference then your destructors will
NOT run!
assign by reference, meaning =& and not just =. If you do not assign by reference then your destructors will
NOT run!