Hi guys
Is it possible to access an object of one class, from inside another class?
Bascially I have a dbconnect class, I start this iwth ...
$db = new dbconnect();
Now I have a form class, in this I want to access the dblink which is the mysql connection ...
$db->dblink;
I thought this would be possible but cant get it to work for some reason.
Is it possible?
Thx
Accesing class var from different class
Moderator: General Moderators
What errors is it giving you?
It is definately possible. Make sure you require() the class before you try to instantiate it.
It is definately possible. Make sure you require() the class before you try to instantiate it.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Hi
Thx for the reply
Here is the class construct that creates the link ...
and my session_handler should see the link ...
This is the error i,m getting
Notice: Undefined variable: db in D:\htdocs\classes\session_handler.php on line 30
Notice: Trying to get property of non-object in D:\htdocs\classes\session_handler.php on line 30
Error - sessions seem to be malfunctioning
Thx again
Thx for the reply
Here is the class construct that creates the link ...
Code: Select all
function __construct() {
$this->db_link = @mysqli_connect ($this->db_host, $this->db_user, $this->db_pass, $this->db_dbname);
if (!$this->db_link) {
exit ("Error - Database seem's to be malfunctioning");
}
}Code: Select all
function _open($path, $name) {
$this->sess_link = $db->db_link;
if (!$this->sess_link) {
exit ("Error - sessions seem to be malfunctioning");
}
return TRUE;
}Notice: Undefined variable: db in D:\htdocs\classes\session_handler.php on line 30
Notice: Trying to get property of non-object in D:\htdocs\classes\session_handler.php on line 30
Error - sessions seem to be malfunctioning
Thx again
well generally there are to approaches to this one.
besides these two..you can use a registry class to store the object...and use it anywhere after that...kind of like the global stuff...but not quite 
Code: Select all
//add $db to global scope..so you see it withing function....bad stuff.
function _open($path, $name) {
global $db;
$this->sess_link = $db->db_link;
if (!$this->sess_link) {
exit ("Error - sessions seem to be malfunctioning");
}
return TRUE;
}
//add as param. recommended
function _open($path, $name,$db) {
$this->sess_link = $db->db_link;
if (!$this->sess_link) {
exit ("Error - sessions seem to be malfunctioning");
}
return TRUE;
}- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia