Forum Moderators: coopster

Message Too Old, No Replies

How to use a Class in a Class

         

cient

8:26 am on Jan 20, 2005 (gmt 0)

10+ Year Member



How to use a Class in da Class?

i have a database class for querying the db, and a counter class who should also be able to query the db.
if i use the counter class it should create a db object
like this:


class counter {
var $db;

function counter() {
$this->db = new CDatabase;
}

function a_query_function() {
$this->db->query("some SQL");
}
}

but the command
$this->db->query("some SQL");
doesn't work because of pointing, c syntax like
($this->db)->query("some SQL");
doesn't work too,

what is the cleanest solution?

coopster

1:54 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, cient.

I would recommend pulling down the PEAR DB class for some analysis. Not only is a functional and proven class, I think you'll learn quite a bit by doing so. Best regards, coopster.

ergophobe

5:29 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One way:

class Counter extends [us4.php.net] Db
{

define Counter class here

}

$counter = new Counter();

$counter->query("SELECT blah blah blah");

Another way

class Counter
$var db;

function Counter(&$db)
{
$this->db = $db;
}
}

$db = new Db();
$ctr = new Counter($db);
$ctr->db->query("SELECT blah blah blah");