Re: Class within a class
| From: | Tom Rogers | Date: | Tue, 11 Feb 2003 03:48:12 +0000 |
| Subject: | Re: Class within a class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-135152@lists.php.net to get a copy of this message | ||
Hi,
Tuesday, February 11, 2003, 5:36:22 AM, you wrote:
JM> I have made a Mysql database class. I wanted to know how I could use
JM> that class in another class without using extend. For example:
JM> include 'db.php';
JM> $db = new db();
JM> ...some php code...
JM> class blah {
JM> var $test = 1;
JM> function blah() {
JM> ......
JM> $db->query(some query); //mysql class being used here
JM> }
JM> }
JM> I don't want to create an instance of db class in the blah class because
JM> I use db in other parts of the script so an instance already exists. Any
JM> way I can do this?
I do it this way
$class_ref = array()
class db{
function db(){
global $class_ref;
$class_ref['db'] =& $this;
}
}
class a {
var $db;
function a{
global $class_ref;
if(isset($class_ref['db'])){
$this->db =& $class_ref['db'];
}else{
$this->db = new db();
}
}
}
using $class_ref you can register all classes thay may be shared.
--
regards,
Tom