Page 1 of 1

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

Posted: Mon Jul 02, 2007 12:07 am
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

Posted: Mon Jul 02, 2007 1:05 am
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'];
}

}