Re: var static
| From: | Alvaro Martinez | Date: | Thu, 28 Aug 2003 18:43:33 +0000 |
| Subject: | Re: var static | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-161072@lists.php.net to get a copy of this message | ||
I've found the solution myself.
The db class is the next:
class db{
function db (){
// funcion que se conecta con la BBDD
$result = @mysql_pconnect("inforalv", "discoteca", "password");
if (!$result)
return false;
if (!@mysql_select_db("discoteca"))
return false;
}
function &getInstancia(){
static $miInstancia;
if (!get_class($miInstancia) == 'db')
$miInstancia=new db();
return $miInstancia;
}
and the call to this class is the next:
$conexiondb1=&db::getInstancia();
$conexiondb2=&db::getInstancia();
In the second call I obtain the same object.
Muchas gracias por vuestra ayuda
Alvaro
"Dynamical.Biz" <info@dynamical.biz> escribió en el mensaje
news:JGELLHNBALPLHKGJDDIMKEMPCHAA.info@dynamical.biz...
Although PHP supports static variables in functions (see here), it has no
support for static variables in classes.
http://www.php.net/manual/en/language.variables.scope.php
(espero que te sirva)
saludos
aniceto lópez :: DYNAMICAL.BIZ
web development & host services
Barcelona - Spain
-----Mensaje original-----
Asunto: [PHP] var static
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php