Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

PEAR Primer
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!

<?php
  
require_once('PEAR.php');
  require_once(
'DB.php');
  require_once(
'Log.php');

  
/**
  * Default PEAR DSN
  *
  * @author Joe Stump <joe@joestump.net>
  * @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 <joe@joestump.net>
  */
  
class Base extends PEAR
  
{
    
/**
    * DB Class
    *
    * @author Joe Stump <joe@joestump.net>
    * @access public
    */
    
var $db;

    
/**
    * Log Class
    *
    * @author Joe Stump <joe@joestump.net>
    * @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 <joe@joestump.net>
    * @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 <joe@joestump.net>
    * @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.
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!
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
One (more) small flawStevenv Vasilogianis04/26/03 02:51
RE: PHPLIB vs. PEARDavid Grinberg02/06/03 22:59
One small flawWouter Roosendaal02/01/03 12:04
PHP Manual "Classes and Objects"daniel @drian keeney01/31/03 23:58
PHPLIB vs. PEARRichard Ruiter01/25/03 08:24
DB InconsistencyMark Tudor01/21/03 18:20
DB InconsistencyMark Tudor01/21/03 18:20
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.