Hi, Friday, August 29, 2003, 3:01:45 AM, you wrote: AM> I want to obtain only one instance of one class. In other to do that, I call AM> to a static method, that creates the instance(if it doesnt exit) or returns AM> the reference of the instance. AM> The call to the static method is the next:
AM> $conexiondb=db::getInstancia(); AM> Well, but if I call to db::getInstancia() another time, I obtain another new AM> object :-( AM> The code of the db class is the next (it is a Singleton Pattern, but it AM> doesnt work) AM> class db{ AM> var $_miInstancia; AM> function db (){ AM> // funcion que se conecta con la BBDD AM> static $miInstancia; AM> $this->_miInstancia=&$miInstancia; AM> $result = @mysql_pconnect("inforalv", "discoteca", "password"); AM> if (!$result) AM> return false; AM> if ([EMAIL PROTECTED]("discoteca")) AM> return false; AM> } AM> &function getInstancia(){ AM> if (!isset($this)) AM> $_miInstancia=new db(); AM> return $_miInstancia; AM> } AM> } AM> I think that the problem is that the var _miInstance is not static and I AM> dont know how to do it. AM> Could you please tell me if there is anything wrong? AM> Thanks. AM> Alvaro static calls to a class will never have $this set so you have to set your reference globally something like this: <?php $_miInstancia = array(); class db{ var $c; function db ($count=false){ global $_miInstancia; $_miInstancia['db'] =& $this; if($count)$this->c = $count; // funcion que se conecta con la BBDD $result = @mysql_pconnect("inforalv", "discoteca", "password"); if (!$result) return false; if ([EMAIL PROTECTED]("discoteca")) return false; } function &getInstancia($count=false){ global $_miInstancia; if (!isset($_miInstancia['db'])){ new db($count); } return $_miInstancia; } } $conexiondb =& db::getInstancia(1); print_r($conexiondb); $conexiondb2 =& db::getInstancia(); print_r($conexiondb2); ?> (The $count was just to prove we only have the one instance) -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php