Alvaro Martinez wrote:
I have written this code:

class db{
       var $_miInstancia;

       function db (){
          // funcion que se conecta con la BBDD
             static $miInstancia;
             $this->_miInstancia=&$miInstancia;

             $result = @mysql_pconnect("inforalv", "discoteca", "password");
             if (!$result)
               return false;
             if ([EMAIL PROTECTED]("discoteca"))
               return false;
       }

function getInstancia(){
   if (!isset($this))
      $_miInstancia=new db();
   return $_miInstancia;
}
}

This is a Singleton Pattern, but it doesnt work.
I want to obtain only one instance of db class but when I do this...:

   $conexiondb=db::getInstancia();
   $conexiondb=db::getInstancia();

In the two calls I get a new db object.
Could you please tell me if there is anything wrong?

two mistakes - you need to use &, and the static var must be in getInstancia()


function &getInstancia(){
    static $miInstancia;
    if (!get_class($miInstancia) == 'db') {
        $miInstancia = &new db();
    }
    return $miInstancia;
}

$conexiondb = &db::getInstancia();

Regards,
Greg
--
http://www.phpdoc.org
phpDocumentor

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



Reply via email to