Storing a reference to an object in another object
| From: | Rory McKinley | Date: | Wed, 19 Jan 2005 14:05:13 +0000 |
| Subject: | Storing a reference to an object in another object | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-206746@lists.php.net to get a copy of this message | ||
Hi All
This is probably just a case of me being Mr Thicky, but maybe someone can point out the error mf my ways:
I have two classes, call them admin and module. Admin stores login details as well as a pointer to the DB connection. Module will run various queries. To do that it needs access to the DB connection maintained by Admin. Because I don't want to have to keep on passing the reference to the admin instance, I thought I would store the reference to the admin instance within the module instance when the object is created:
class Module
{
private $admin_instance;
function __construct($admin_instance)
{
$this->admin_instance = $admin_instance;
}
public function checkConnection()
{
return $this->admin_instance->checkConnection()
}
}
I am using PHP 5 so the reference should be passed and stored in the relevant object attribute, not so?
Yet, if i call checkConnection it tells me that I am calling a method on a non-object.print_r() of the module instance makes things even more confusing: print_r returns:
[admin_instance:private] => [class_name] => blah [admin_instance] => Admin Object
which is followed by the various attributes of the admin object suggesting that the reference to the admin object is indeed within the module instance.
I hope someone can help with this..
Oh, and before anybody asks I am still busy RTFM, STFW, STFA but with no joy so far.
--
Rory McKinley
Nebula Solutions
+27 21 555 3227 - office
+27 21 551 0676 - fax
+27 82 857 2391 - mobile
www.nebula.co.za
====================