var static
| From: | Alvaro Martinez | Date: | Thu, 28 Aug 2003 17:01:45 +0000 |
| Subject: | var static | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-161063@lists.php.net to get a copy of this message | ||
I want to obtain only one instance of one class. In other to do that, I call
to a static method, that creates the instance(if it doesnt exit) or returns
the reference of the instance.
The call to the static method is the next:
$conexiondb=db::getInstancia();
Well, but if I call to db::getInstancia() another time, I obtain another new
object :-(
The code of the db class is the next (it is a Singleton Pattern, but it
doesnt work)
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 (!@mysql_select_db("discoteca"))
return false;
}
&function getInstancia(){
if (!isset($this))
$_miInstancia=new db();
return $_miInstancia;
}
}
I think that the problem is that the var _miInstance is not static and I
dont know how to do it.
Could you please tell me if there is anything wrong?
Thanks.
Alvaro