Re: global class instance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



sempsteen wrote:
yes i'm calling a lot. actually i have a class that handles mysql
queries named "database".
what i want was call a database method from a method of another function.

class
....
  function
  ...$database->execute_query(...

Always CC the list.

What's wrong with this being a global variable?

In your common or global or whatever you call it file:

$db = &new Db();
$db->Connect('....');
$GLOBALS['Database'] = &$db;

then you use $GLOBALS['Database']->...

This file is *always* called before your other classes so it's always set. It can't be overridden or anything, so I don't understand the objection to it being a global variable.


Another option, inside your main class (you're using inheritance right?)

var $Db = null;

function GetDb() {
  if (!is_null($this->Db) && is_resource($this->Db->Connection)) {
    return $this->Db;
  }
  $db = & new Db();
  $db->Connect(...);
  $this->Db = &$db;
}

then use

$db = $this->GetDb();

inside your class.

--
Postgresql & php tutorials
http://www.designmagick.com/

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux