if (!is_a ($miInstancia, 'db')) $miInstancia=new db();
That way you can ensure that the variable has been instantiated and is an instance of the db class.. But it may not really matter..
Jordan S. Jones
Alvaro Martinez wrote:
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 ([EMAIL PROTECTED]("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" <[EMAIL PROTECTED]> escribió en el mensaje news:[EMAIL PROTECTED]
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 ([EMAIL PROTECTED]("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
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php