Classes newbie - how to access an object from within another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Classes newbie - how to access an object from within another

Post 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.) :?:
rainman
Forum Newbie
Posts: 2
Joined: Thu Jul 03, 2003 11:46 pm

Classes newbie - how to access an object from within another

Post 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.
zebrax
Forum Newbie
Posts: 16
Joined: Sat Sep 14, 2002 11:39 pm

Re: Classes newbie - how to access an object from within ano

Post 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();
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Re: Classes newbie - how to access an object from within ano

Post 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.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Just some FYI. What Zebrax showed you is called message passing.

Cheers,
BDKR
Post Reply