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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to