[SOLVED] Classes and scope
Posted: Wed Dec 08, 2004 6:44 pm
Ok here is the scenario. I have one class, lets call it 'dad' and another class 'junior'. Either 'dad' or 'junior' can be used directly from various scripts. If dad is used, he will create a bunch of 'juniors' (heh).
I also have a class 'db' which is a database abstraction class.
The 'dad' and 'junior' classes will be called from scripts which may or may not be using the db abstraction class so I can't assume that I will be passed this class.
So whats the best way to even do this? Is just passing it to the constructor good? What if I pass it by reference (thats what I think is best). Is there another way to make the 'db' class accessible to everyone? I dont want to automatically build a 'db' class in everyone of these, because it will create a lot of overhead.
is it right to do something like this:
*sigh* I'm not sure what to do....
I also have a class 'db' which is a database abstraction class.
The 'dad' and 'junior' classes will be called from scripts which may or may not be using the db abstraction class so I can't assume that I will be passed this class.
So whats the best way to even do this? Is just passing it to the constructor good? What if I pass it by reference (thats what I think is best). Is there another way to make the 'db' class accessible to everyone? I dont want to automatically build a 'db' class in everyone of these, because it will create a lot of overhead.
is it right to do something like this:
Code: Select all
<?php
class dad
{
var $db;
function dad(&$thisDB)
{
if(!is_object($thisDB))
{
include(db.class.php)
$this->db = new db($connectinfo)
}
else
$this->db = $thisDB
}
}
?>