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 ConnectionFactory
{
var $dbAccessor;
var $host = "myhost";
var $database = "mydbname";
var $user = "root";
var $password = "none";
function ConnectionFactory($host= '', $database = '', $user= '',
$password='')
{
$this->dbAccessor =
DB::connect('mysql://'.$user.'@'.$password.'+'.$host.'/'.$database);
}
function getInstance()
{
//single instance
global $instance;
if(!isset($instance))
{
$instance = new ConnectionFactory($this->host,
$this->database,$this->user, $this->password);
}
return($instance);
}
function getAccessor()
{
return $this->dbAccessor;
}
}
-----------
To use it, I call:
$singleton =& ConnectionFactory::getInstance();
$db=$singleton->getAccessor();
$row = $db->getRow("select * from user");
Thanks for your help,
Bob 3 1724
"Bob" <ro**********@consultant.com> wrote in message
news:de**************************@posting.google.c om... 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):
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
"Bob" <ro**********@consultant.com> wrote in message
news:de**************************@posting.google.c om... 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.
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.
"Virgil Green" <vj*@DESPAMobsydian.com> wrote in message news:<lX*****************@newssvr22.news.prodigy.c om>... 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?
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!!
Bob This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Medi Montaseri |
last post by:
Hi,
Given a collection of similar but not exact entities (or products)
Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern
to provide a common interface to these products....
|
by: Eric |
last post by:
Perhaps this question has been posed before (I'd be surprised if it
hasn't) but I just gotta know...
Is it possible to combine the Singleton and Factory Method design
patterns in the same class?...
|
by: Paramesh |
last post by:
Hello friends,
My friend asked me this question:
This question regards proprietary software (of which I am one of the
developers), so I cannot post actual code for this question. I will try...
|
by: Craig Buchanan |
last post by:
I've seen design patterns for class factories that work well to create
(fetch) objects, but I haven't seen anything about how to persist the class'
data when it has changed. Is this done thru the...
|
by: Dave |
last post by:
Hello all,
Please see my question embedded in comment form below.
Thanks,
Dave
#include <iostream>
#include <boost/shared_ptr.hpp>
| |
by: Anders Borum |
last post by:
Hello!
Whilst refactoring an application, I was looking at optimizing a
ModelFactory with generics. Unfortunately, the business objects created by
the ModelFactory doesn't provide public...
|
by: ma740988 |
last post by:
Consider:
#include "handyfactory.h"
#include <iostream>
struct Shape
{
virtual void print() const=0;
};
|
by: GarrettD78 |
last post by:
Accidently posted this to the wrong group so I am reposting.
This is probably a newbie question but I am a little confused about how to go
next with my code. I think I want to use a factory pattern...
|
by: neoairus |
last post by:
I'm developing a pseudo-library for neural network. To simplify
librarary using i wont to implement a sistem to instantiate different
implementation of interface(Layer.h Neuron.h) passing a string...
|
by: CSharper |
last post by:
I have created a Factory pattern code. One thing I noticed after
coding, each factory has some methods they are exactly same and doing
the same code using the values specific to each factory, if I...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |