php5 3-tier architecture

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

Threaded View
I'm newbie to php, trying to find the best design for data access
classes.  What is the best way to securely handle database login
information?  Also, when retrieving multiple result sets, I want to
keep access to the mysqli object restricted to the data access class.
Is it best to return an array of result sets, or is there another way?
I've included my current script below, with examples of both of the
problems I've mentioned.  Thanks in advance for any advice.

//--------------------Begin Data
class DataService {

//database connection
private $mysqli;

//default constructor
public function __construct () {
$this->mysqli = new mysqli("localhost");

//default destructor
public function __destruct () {

//gets all faculty members
public function getAllFaculty () {
$this->mysqli->multi_query ($query) or die ('Query Failed.' .
$i = 0;
do {
$results[$i] = $this->mysqli->store_result();

} while($this->mysqli->next_result());

return $results;
//------------------End Data

Re: php5 3-tier architecture

Hi ruselw,

Imho in a 3 tier architecture you would have a class Faculty with a
static method getAllInstances() (or a FacultyFactory object whith such a
method) that retrieves the faculty data from a database abstraction
layer object like your DataService and returns the corresponding
instances of Faculty.

Your DataService object may return an array with the data from the query
(like you do), or it may have a getNext method to get the data record by
record. The last is probably more efficient if the result is passed by
reference. This may expose the database cursor, but that should not be
such a problem because the proper way to use the DataService is to call
the Faculty::getAllInstances() method, which copies the data anyway.


Henk Verhoeven, wrote:

Quoted text here. Click to load it

Re: php5 3-tier architecture wrote:
Quoted text here. Click to load it

Lots of ways to skin that cat.

You could return an iterator. (PHP5)

You could pass a collection type object in and out for pagination, that
collection object would have a startPos(), pageSize(), addRecord(), isFull(),
getList() style methods. Then your database abstraction layer populates the
collection. (With an underlying array of records)

This has advantages when it comes to pagination, but is difficult when you need
all the results.

For extreme cases, you could introduce another tier with some form of RPC, this
would encapsulate all your business logic and keep your database completely
hidden from the web front end.

It also solves persistant database handle problems. The middle tier implements
a pool of database connections, you talk to it in a stateless manner when you
need access to the database, this frees the database handles for use in other
page requests. (Not so much an issue with mysql, but can be an issue with
DB2 or databases that offer a limited number of connections due to license

PHP, or even HTTP may not be the best choice for the middle tier. It's
complicated and should be avoided in cases where it's not required.

I wouldn't worry too much about allowing access to a mysql handle, in fact,
I might even have a getHandle() type of method for getting it, simply because
another 3rd party class might need it and it's better than having 2 connections
to the database for 1 request.

--                     Custom web programming
guhzo_42@lnubb.pbz (rot13)                User Management Solutions

Site Timeline