OOP and Pear::DB

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi folks-

I am currently working on a PHP application which I am using as a
personal learning environment for getting a better working
understanding about how to tackle application development in an OOP/MVC
pattern fashion.

I am using PEAR::DB to create database connection objects, and my major
question concerns where these objects should be created.

For example: Let's say I have a login application that that has a
controller-like script that looks something like this:

// login.php

$user = new $User;

....and a model-like User class that looks something like this: (The
important note is that I may need to load data multiple times from a

// User.class.php

class User
    public $user_name;
    public $user_id;
    public $admin_rights = array();
    protected $db_connection;

    function __construct()
        if (!this->db_connection){
            $this->db_connection = DB::connect(DB_DSN);

        $result = $this->dbh->getAll($sql, array($this->request_id);
        $this->user_name = $result['USERNAME'];
        $this->user_id   = $result['USER_ID'];

    function getAdminRights()
        $this->admin_rights = $this->dbh->getAll($sql,


As you may see, I am creating an instance of a database object as an
attribute of the User class itself.

This seems to have benefits like:
-- Keeps data manipulation out of the controller (and away from having
to global a db object if I had created it on the login.php page)

And also drawbacks like:
-- Not exactly an example of loose coupling in the above example.

I'd greatly appreciate some feedback from experienced PHP OOP
developers about the best way to tackle this problem. :)

Where to create database objects for use with classes?


Shawn C.

Re: OOP and Pear::DB

scoomey wrote:

Quoted text here. Click to load it

I don't know if I'm qualified to speak as an 'experienced PHP OOP
developer', but I usually create one database object before
instantiating my own objects and explicitly pass references to it to my
objects. E.g. (PHP 4 style):


$db_object =& DB::connect(DB_DSN);
$some_object =& new Foo($db_object);


(somewhere else)

class Foo {

    function Foo(&$db_object) {
       $this->db_object =& $db_object;


Real e-mail address unavailable. 5000+ spams per month.

Re: OOP and Pear::DB

Have a common base class for all your classes that represents rows in
database tables. This base class should then have a static method that
returns a db connection object. Eg.:

class DBObject {
    * @param array
    public function __construct($id) {
        $this->_dbConnection = $this->getDBConnection();
        $this->_id = $id;

    public static function getDBConnection() {
        Fetch and return your db connection, that you may have eg. in the
global scope.
    protected $_dbConnection;
    protected $_id;

class User extends DBObject {
    public function getName() {
        $query = 'SELECT name FROM User WHERE userID = ' . $this->_id[0];
        return $this->_dbConnection->GetOne($query);



Quoted text here. Click to load it

Re: OOP and Pear::DB

Wow, thanks for the responses. Peter, this method is very enlightening.
I didn't think to take the database as the base class and extend my
business logic objects from it. Now it seems to make perfect sense.

Site Timeline