How to connect all the class files to access data easily?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

How to connect all the class files to access data easily?

Post by shivam0101 »

wrote 3 separate class. Admin, Member and Sql

The Sql class contains all the methods of sql statements, like INSERT, DELETE etc.

The Admin contains all the methods which are needed for admin

The Member contains all the methods which are needed for Member

How to connect all? Currenty i created a class called Cls in which i have declared all the methods which are in the 3 classes (Admin, Member and Sql), and redirecting to the relevant class.

for example,

to access member name, which is defined in Member class i will be calling,

Code: Select all

php: 
$member_obj=new Cls; 
$member_obj->memberName($id);


In the Cls class,
php:

Code: Select all

function MemberName($id) 
{ 
    return Member :: MemberName($id); 
}
In the Member class,
php:

Code: Select all

function MemberName($id) 
{ 
    $name=Sql :: Get($tblName, "WHERE id=$id"); 
    return $name['member_name']; 
}



In the Sql class,
php:

Code: Select all

function Get($tblName, $cond) 
{ 
  $query=mysql_query("SELECT * FROM $tblName WHERE $cond"); 
  return mysql_fetch_row($query); 
}
Which looks clumsy, It should be linked in a better way, can some one give me some idea to link and call the class
Last edited by shivam0101 on Mon Jul 02, 2007 1:18 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Composition can help you here. Pass object that a class needs to the constructor and use them internally like this:

Code: Select all

class Member {
private $sql;

function _construct($sql)
{
     $this->sql = $sql;
}

function MemberName($id)
{
     $name = $this->sql->Get($tblName, "WHERE id=$id");
     return $name['member_name'];
}

}
(#10850)
Post Reply