Hello!
And first of all thanks to all php gurus who helped me a lot last time
on my question!
I have another problem now!
I have taken a Database Connection Factory class to use in my website.
Normally, I get a singleton of the Database instance.
What's wrong is that this singleton is initialized for every page
where i call it.
If I use the singleton several times in a same php web page, it is
initialized once only, it works fine.
But then every time I load a new php web page, if I put some log, I
can see the singleton is new again and initialized every time!
I thuoght that the point of a factory class was to initialize only
once an important variable.
Did I understand wrong?
Here is the code (very simple):
class ConnectionFacto ry
{
var $dbAccessor;
var $host = "myhost";
var $database = "mydbname";
var $user = "root";
var $password = "none";
function ConnectionFacto ry($host= '', $database = '', $user= '',
$password='')
{
$this->dbAccessor =
DB::connect('my sql://'.$user.'@'.$pa ssword.'+'.$hos t.'/'.$database);
}
function getInstance()
{
//single instance
global $instance;
if(!isset($inst ance))
{
$instance = new ConnectionFacto ry($this->host,
$this->database,$th is->user, $this->password);
}
return($instanc e);
}
function getAccessor()
{
return $this->dbAccessor;
}
}
-----------
To use it, I call:
$singleton =& ConnectionFacto ry::getInstance ();
$db=$singleton->getAccessor( );
$row = $db->getRow("sele ct * from user");
Thanks for your help,
Bob
And first of all thanks to all php gurus who helped me a lot last time
on my question!
I have another problem now!
I have taken a Database Connection Factory class to use in my website.
Normally, I get a singleton of the Database instance.
What's wrong is that this singleton is initialized for every page
where i call it.
If I use the singleton several times in a same php web page, it is
initialized once only, it works fine.
But then every time I load a new php web page, if I put some log, I
can see the singleton is new again and initialized every time!
I thuoght that the point of a factory class was to initialize only
once an important variable.
Did I understand wrong?
Here is the code (very simple):
class ConnectionFacto ry
{
var $dbAccessor;
var $host = "myhost";
var $database = "mydbname";
var $user = "root";
var $password = "none";
function ConnectionFacto ry($host= '', $database = '', $user= '',
$password='')
{
$this->dbAccessor =
DB::connect('my sql://'.$user.'@'.$pa ssword.'+'.$hos t.'/'.$database);
}
function getInstance()
{
//single instance
global $instance;
if(!isset($inst ance))
{
$instance = new ConnectionFacto ry($this->host,
$this->database,$th is->user, $this->password);
}
return($instanc e);
}
function getAccessor()
{
return $this->dbAccessor;
}
}
-----------
To use it, I call:
$singleton =& ConnectionFacto ry::getInstance ();
$db=$singleton->getAccessor( );
$row = $db->getRow("sele ct * from user");
Thanks for your help,
Bob
Comment