Factory class question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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 ConnectionFactory

 var $dbAccessor;
 var $host     = "myhost";
 var $database = "mydbname";
 var $user = "root";
 var $password = "none";

 function ConnectionFactory($host= '', $database = '', $user= '',
 $this->dbAccessor =

 function getInstance()

  //single instance
  global $instance;

   $instance = new ConnectionFactory($this->host,
$this->database,$this->user, $this->password);


 function getAccessor()
  return $this->dbAccessor;


To use it, I call:

    $singleton =& ConnectionFactory::getInstance();

    $row = $db->getRow("select * from user");

Thanks for your help,

Re: Factory class question

Quoted text here. Click to load it

Yes, and that why it's pretty pointless to use class factories in PHP.
Basically the problem that you're trying to solve is already solved by PHP
internally and your code has little to do. Just call the appropriate DB
connection function and let PHP's resource management do its thing.

Re: Factory class question

Quoted text here. Click to load it

PHP? Web? everything is new every time you load a page. That's the nature of
the beast. Sessions, databases, filesystems, cookies, etc. are used to
oversome the transactional nature of the web.

Are you, perhaps thinking along the lines of Java and Servlet containers?

 - Virgil

Re: Factory class question

Quoted text here. Click to load it

Indeed, I've been programming in java for web all my programmer's
life, so I thought I could find some similar concept in php!

I wanted to have a factory for some cache of some datas that are
shared among my website users.
I wanted to use an object that could contain some datas from a user,
and others users could get the info, instead of reloading them from
the database each time.
What I am going to do is to use Cache_Lite from Pear which seems to be
good for that job!

Thank you all for your help!!

Site Timeline