473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

OOP and Pear::DB

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
<?php
require_once('User.class.php');

$user = new $User;
$user->getAdminRights();
?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

....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
database.)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// User.class.php
<?php

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

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

$sql = "SELECT USERNAME, USER_ID FROM SOME_TABLE...";
$result = $this->dbh->getAll($sql, array($this->request_id);
$this->user_name = $result['USERNAME'];
$this->user_id = $result['USER_ID'];
}

function getAdminRights()
{
$sql = "SELECT * FROM SOME_TABLE WHERE ADMIN_ID = ?";
$this->admin_rights = $this->dbh->getAll($sql,
array($this->user_id);
}

}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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?

TIA-

Shawn C.

Jul 17 '05 #1
3 1864
scoomey wrote:
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?


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):

(index.php):

$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;
}
}

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #2
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);
}
}

Regards,

Peter

"scoomey" <sc*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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
<?php
require_once('User.class.php');

$user = new $User;
$user->getAdminRights();
?>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

...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
database.)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// User.class.php
<?php

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

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

$sql = "SELECT USERNAME, USER_ID FROM SOME_TABLE...";
$result = $this->dbh->getAll($sql, array($this->request_id);
$this->user_name = $result['USERNAME'];
$this->user_id = $result['USER_ID'];
}

function getAdminRights()
{
$sql = "SELECT * FROM SOME_TABLE WHERE ADMIN_ID = ?";
$this->admin_rights = $this->dbh->getAll($sql,
array($this->user_id);
}

}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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?

TIA-

Shawn C.

Jul 17 '05 #3
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.
Thanks.

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: kain | last post by:
hi there, I'm currently converting a MySQL application to PostgreSQL, and I hacked this app to support PEAR::Db . after some good coding days, I've found a problem: mysql_insert_id on pear::db ....
0
by: Analysis&Solutions | last post by:
Greetings: Crack open the beer, PEAR DB 1.6.0 is here! (Hey, I'm a bit giddy with excitement that my intense work during the past seven weeks has come to fruition.) For those unfamiliar with...
3
by: Wayne Smallman | last post by:
Hi! I've had a look through various forums for some advice on getting Pear DB working with my Entropy installation of PHP Version 4.3.6 Most of the topics I found are after the fact, so that's...
1
by: James | last post by:
Hey everyone, I have a really obscure but impassable problem with a reasonably simple piece of php/PEAR DB/MySQL code When calling several stored procs the first call succeeds, but subsequent...
8
by: lawrence k | last post by:
I wrote a simple CMS for personal use. I'm thinking of using it for other clients now. It's use of the database is slow and inefficient. I'm thinking of switching to the PEAR class listed here: ...
1
by: Paul | last post by:
I recently upgraded from MySQL 3.23 to 4.1. Now db is not working properly. I'd very much like your help in solving this issue! Here's the code I used to test it: require_once 'DB.php'; $db...
4
by: andy | last post by:
Hey All, When I have DB.php (PEAR) in the root directory of my project it works ok using require "DB.php"; When I put it in a seperate folder and call it using require "classes/DB.php";
3
by: mpar612 | last post by:
I am stumped. Below is my code and it doesn't work (I removed the $login info for posting). PHP Code: require 'PEAR/DB.php'; // Connect to the database $db = DB::connect($login); if...
5
by: jmark | last post by:
I need some assistance on how to create a select statement using PEAR DB or MySQL with the LIKE function. That is I am looking of how I can get statement like this work. SELECT * FROM customer...
2
by: daralthus | last post by:
Hello! I would like to ask your help, i have founded a great code here: http://www.jamescaws.co.uk/2008/07/dynamically-count-exit-link-clicks-throughs-using-javascript-php/ but it uses Pear DB...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.