Re: global class instance

From: Date: Mon, 03 Jul 2006 00:13:02 +0000
Subject: Re: global class instance
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-239075@lists.php.net to get a copy of this message
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/

« previous php.general (#239075) next »