Re: real world singleton class example?



Erwin Moller wrote:
Jerry Stuckle wrote:


Erwin Moller wrote:

Jerry Stuckle wrote:



d.adamkiewicz@xxxxxxxxx wrote:


Hello Folks

Anybody can show me real world singleton class example?
Something that works (is implemented) as part of working solution.
Does it make sense to create database handler class that way?

Regards
Darek


Darek,

I've used them for database classes before. It allows me to have
multiple classes (representing my business-layer logic) using the same
instance without worrying about whether the object has been previously
defined or used.

Because of the transactional nature of web pages, they aren't as useful
as in other programs. But they still have a use.



Jerry,

We once had a 'discussion' about this that ended in a ... well... not too
nice. :-)
So let me try again. (I am not trying to start flaming again, but I am
curious because I am afraid I am missing something.)

My confusion/question:
What is the use of a singleton is when running PHP?
I completely understand (and used) the concept in Java (J2EE), but I fail
to see the advantage of a singleton in PHP, since PHP is executing
'per-page'.

I can see the advantage of PHP connectionpooling (behind the scenes), but
why make a singleton for db-connect?
As far as I understand, you can use the same connection many times on the
same page, so what is the singleton adding to it?

TIA!

Regrads,
Erwin Moller

Hi, Erwin,

Ok, let's take the following scenario - I'll keep it to the
database/table level to keep it simple; normally I would have business
object which access multiple tables instead of mimicking the tables
themselves. But in a more complicated process I would have the business
objects on a layer above the tables

class database1
constructor
connect()
disconnect()

class table1
constructor
insert()
remove()
query()

class table2
constructor
insert()
remove()
query()

Now,

table1 and table2 tables within the database. On any single page you
may use table1, table2 or both.

You could create a database1 object and pass it to the constructor of
table1 and table2, but that would be needless overhead in the program -
it shouldn't have to worry about database1, only the table1 and table2
it needs.

You could have table1 and table2 each connect to the database, but where
you use both tables on the same page, that's a needless extra connection.

So, you create a getSingleton() member in database1. Now the
constructors for table1 and table2 can call database1::getSingleton();
if an object of the class doesn't exist, it is created. If it does
exist, the object is reused.

Does this help?



Hi,

That makes sense in your described setup.
Allthough passing the connectionObject around can be used too (as you described).
Thanks for the input.

Regards,
Erwin Moller




Yes, you can pass around a connectionObject. But arguably this places an additional, unnecessary requirement on the program itself.

Preferably, the program shouldn't have to worry about passing around the connectionObject if the program has no need for it. IOW, the classes should handle what they can - and only require data from the program which only the program can supply (i.e. search parameters).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.



Relevant Pages

  • Re: real world singleton class example?
    ... I've used them for database classes before. ... What is the use of a singleton is when running PHP? ... table1 and table2 tables within the database. ...
    (comp.lang.php)
  • Re: real world singleton class example?
    ... I've used them for database classes before. ... What is the use of a singleton is when running PHP? ... table1 and table2 tables within the database. ...
    (comp.lang.php)
  • Re: Move record from one table to another
    ... Building a database isn't like building ... Set a "pushFlag" column in the record in table1 to a specific ... Sets the "status" field in the new record in table2 to a specific ... If a duplicate is not found, ...
    (comp.databases.ms-access)
  • Re: Noob question on Access table merging
    ... database, some of the items I already have in my database. ... Now create a query with Table1 and Table2. ... Dbl-Click on it and select the option to select All records from Table1 and only those that match in Table2. ...
    (comp.databases.ms-access)
  • Re: counting in a query
    ... Say you've got two instances of 12345 in table1 ... and only one in table2. ... >>> How do I get it to give me a count of the zip5 field in each database ... >>> Joe Robison ...
    (microsoft.public.fox.programmer.exchange)