Putting It All Together!
Now that you basically know how to use the main PEAR classes and we’ve built our own base class, which is based on
PEAR, we can put it all together in a User class for our site!
<?php
require_once('Base.php');
/**
* User class
*
* A class to retrieve user information and perform basic
* user related tasks.
*
* @author Joe Stump <[email protected]>
*/
class User extends Base
{
/**
* @author Joe Stump <[email protected]>
* @access public
*/
var $userID;
/**
* @author Joe Stump <[email protected]>
* @access public
*/
var $data;
/**
* User constructor
*
* @author Joe Stump <[email protected]>
* @access public
* @param int $userID
* @return void
*/
function User($userID = 0)
{
$this->Base();
if(
$userID)
{
$sql = "SELECT *
FROM users
WHERE userID='$userID'";
$result = $this->db->query($sql);
if(!DB::isError($result) && $result->numRows())
{
$this->data = $result->fetchRow();
$this->userID = $userID;
}
else
{
$this = new PEAR_Error("Invalid userID: $userID");
}
}
else
{
$this->userID = 0;
$this->data = array();
}
}
/**
* isAdmin
*
* Check to see if the userID is in our admins table
*
* @author Joe Stump <[email protected]>
* @access public
* @return bool
*/
function isAdmin()
{
$sql = "SELECT *
FROM admins
WHERE userID='".$this->userID."'";
$result = $this->db->query($sql);
if(DB::isError($result) && $result->numRows())
{
return true;
}
$this->log->log('Failed isAdmin() with userID '.$this->userID.'!');
return false;
}
/**
* User Destructor
*
* @author Joe Stump <[email protected]>
* @access public
* @return void
*/
function _User()
{
$this->_Base();
}
}
?>
As you can see from the above example once you have an instance of your User class you have everything you
need to start manipulating data and doing some advanced debugging. Probably the best thing about working
with PEAR is that it makes debugging your classes a lot easier.
need to start manipulating data and doing some advanced debugging. Probably the best thing about working
with PEAR is that it makes debugging your classes a lot easier.