Page 1 of 1
Classes newbie - how to access an object from within another
Posted: Thu Jul 03, 2003 6:13 pm
by Heavy
Code: Select all
<?php
//scroll down to: //Here I'm lost...
class DB{
var $link;
var $QueryResult;
function DB($strDB_Host, $strDB_User, $strDB_Password, $strDefaultDB){
$this->link = mysql_connect($strDB_Host , $strDB_User , $strDB_Password);
mysql_select_db($strDefaultDB);
}
function query($strSQL){
$result = mysql_query($strSQL,$this->link);
$this->QueryResult = $result;
return $result;
}
}
$DB = new DB('DB_Host', 'DB_User', 'DB_Password', 'DefaultDB');
class B{
function B(){
//Here I'm lost
$DB->query('update something');
}
}
$B = new B;
?>
At "//Here I'm lost", How do I get do access the $DB object?
I think it is by reference in some way, but I don't really get how...
Or should I store the objects in the session like this:?
Code: Select all
<?php
class DB{
var $link;
var $QueryResult;
function DB($strDB_Host, $strDB_User, $strDB_Password, $strDefaultDB){
$this->link = mysql_connect($strDB_Host , $strDB_User , $strDB_Password);
mysql_select_db($strDefaultDB);
}
function query($strSQL){
$result = mysql_query($strSQL,$this->link);
$this->QueryResult = $result;
return $result;
}
}
//Session var instead
$_SESSION['DB'] = new DB('DB_Host', 'DB_User', 'DB_Password', 'DefaultDB');
class B{
function B(){
//Session var instead
$_SESSION['DB']->query('update something');
}
}
//Session var instead
$_SESSION['B'] = new B;
?>
I think this latter example is not very good, since objects that I might not use in any other page are sent along with the session. (If I don't unset it of course.)

Classes newbie - how to access an object from within another
Posted: Thu Jul 03, 2003 11:46 pm
by rainman
Just a couple of lines from where you declared class B,
$DB = new DB('DB_Host', 'DB_User', 'DB_Password', 'DefaultDB');
this creates an object of class DB which $DB will be an instance of it in which you will have access to that class's functions.
it is really like a link/pointer. if you are familiar with other object oriented languages like Java, then its a similar concept.
in java, instead of using ->, it uses the . to call functions.
eg
DB db = new DB('DB_Host', 'DB_User', 'DB_Password', 'DefaultDB');
db.query("update something");
and by calling $DB->query('update something');, you access that object's functions.
Re: Classes newbie - how to access an object from within ano
Posted: Fri Jul 04, 2003 12:31 am
by zebrax
I do somehting like the following
Code: Select all
<?php
class DB{
var $link;
var $QueryResult;
function DB($strDB_Host, $strDB_User, $strDB_Password, $strDefaultDB){
$this->link = mysql_connect($strDB_Host , $strDB_User , $strDB_Password);
mysql_select_db($strDefaultDB);
}
function query($strSQL){
$result = mysql_query($strSQL,$this->link);
$this->QueryResult = $result;
return $result;
}
}
$DB = new DB('DB_Host', 'DB_User', 'DB_Password', 'DefaultDB');
class B{
function B(){
//Here I'm lost
$DB->query('update something');
}
function setDb (&$db )
{
$this->DB = &$db;
}
function doSomething(){
$this->DB->query();
}
$B = new B;
$B->setDb($DB);
?>
You can access all of the DB class functions inside $B by using $this->DB->whaterver();
Re: Classes newbie - how to access an object from within ano
Posted: Fri Jul 04, 2003 11:14 am
by Heavy
zebrax wrote:You can access all of the DB class functions inside $B by using $this->DB->whaterver();
Thanks. I'll try it out when I get back to work again.
Posted: Fri Jul 04, 2003 12:29 pm
by BDKR
Just some FYI. What Zebrax showed you is called message passing.
Cheers,
BDKR