473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Singleton design pattern relveance in PHP

Since the webserver loads a PHP script every time a request is issued
from a client, it seems that Singletons are unnessesary in PHP?, since
each time an object is invoked, it is (guaranteed?) to be the only
instance - at least for that thread that is handling the request ? No ?

If I am missing something quite obvious, please let me know.

Sep 28 '06 #1
4 1509

Bit Byte wrote:
Since the webserver loads a PHP script every time a request is issued
from a client, it seems that Singletons are unnessesary in PHP?, since
each time an object is invoked, it is (guaranteed?) to be the only
instance - at least for that thread that is handling the request ? No ?

If I am missing something quite obvious, please let me know.
<?php
//Thread starts

$a = new Foo();
$b = new Foo();

//Now have two instances of Foo, all in the same thread -- no
guarantees here

//Thread ends
?>

Sep 28 '06 #2


ZeldorBlat wrote:
Bit Byte wrote:
>>Since the webserver loads a PHP script every time a request is issued
from a client, it seems that Singletons are unnessesary in PHP?, since
each time an object is invoked, it is (guaranteed?) to be the only
instance - at least for that thread that is handling the request ? No ?

If I am missing something quite obvious, please let me know.


<?php
//Thread starts

$a = new Foo();
$b = new Foo();

//Now have two instances of Foo, all in the same thread -- no
guarantees here

//Thread ends
?>
Hmm, yes, that was a rather obvious refudiation of the point I was
making. I obviously wasn't making myself clear. What I meant was to say
was that other than in cases (such as the one above) where the user
deliberately creates multiple instances of the same class, I can't see
the relevance of porting this Pattern to PHP - since each request is
handled in a new thread/(possibly) process.

Case in point. I have wrapped up all of my user management functionality
in a usermanger class which derives from a Singleton base class
(following logic I had used in my C++ program). Its only when I started
using this class that it became obvious to me that the logic was not
necessarily meaningful in the PHP space - each request to manage a user
- will (as I understand it) - fork a new process (or maybe just spawn a
new thread), which will have its own singleton class - blisfully unaware
of the other's existence - unless ofcourse - the Singleton objects are
being cached in shmem (shared memory) - otherwise, it all seems a bit
like a pointless exercise in futility (because in C++ etc, once you
create a Singleton, it notmally stays memory resident until program
termination - whereas in PHP, the Singleton is only alive for the
duration of the request?) - unless again, I am missing something.

Sep 29 '06 #3
Bit Byte said the following on 29/09/2006 12:13:
ZeldorBlat wrote:
><?php
//Thread starts

$a = new Foo();
$b = new Foo();

//Now have two instances of Foo, all in the same thread -- no
guarantees here

//Thread ends
?>

Hmm, yes, that was a rather obvious refudiation of the point I was
making. I obviously wasn't making myself clear. What I meant was to say
was that other than in cases (such as the one above) where the user
deliberately creates multiple instances of the same class, I can't see
the relevance of porting this Pattern to PHP - since each request is
handled in a new thread/(possibly) process.
If you're not the only user of your class, then the example code above
is entirely possible. Remember that the whole point of a lot of OOP
semantics is purely to enforce design rules - e.g. public vs. private,
abstract, final, etc. In other words, forcing your users to use your
code as it was designed.

Another scenario might be something like the following contrived scenario:

$objects = array();

foreach ($requests as $item)
{
switch ($item)
{
case ITEM_FOO:
$objects[] = Foo::createFoo();
break;

case ITEM_BAR:
$objects[] = Bar::createBar();
break;
}
}
}

Where $requests is an array containing "requests" for creation of Foo
and Bar objects, which are then collected in the $objects array. In
this scenario, if we only want a single shared instance of Bar, for
instance, we would need to design it as a singleton.

Of course this is contrived, but it demonstrates that situations where
multiple requests for an instance are not necessarily as simple as
ZeldorBlat's example above.
--
Oli
Sep 29 '06 #4
In article <io********************@bt.com>, ro**@yourbox.com says...
>

ZeldorBlat wrote:
Bit Byte wrote:
>Since the webserver loads a PHP script every time a request is issued
from a client, it seems that Singletons are unnessesary in PHP?, since
each time an object is invoked, it is (guaranteed?) to be the only
instance - at least for that thread that is handling the request ? No ?

If I am missing something quite obvious, please let me know.

<?php
//Thread starts

$a = new Foo();
$b = new Foo();

//Now have two instances of Foo, all in the same thread -- no
guarantees here

//Thread ends
?>

Hmm, yes, that was a rather obvious refudiation of the point I was
making. I obviously wasn't making myself clear. What I meant was to say
was that other than in cases (such as the one above) where the user
deliberately creates multiple instances of the same class, I can't see
the relevance of porting this Pattern to PHP - since each request is
handled in a new thread/(possibly) process.

Case in point. I have wrapped up all of my user management functionality
in a usermanger class which derives from a Singleton base class
(following logic I had used in my C++ program). Its only when I started
using this class that it became obvious to me that the logic was not
necessarily meaningful in the PHP space - each request to manage a user
- will (as I understand it) - fork a new process (or maybe just spawn a
new thread), which will have its own singleton class - blisfully unaware
of the other's existence - unless ofcourse - the Singleton objects are
being cached in shmem (shared memory) - otherwise, it all seems a bit
like a pointless exercise in futility (because in C++ etc, once you
create a Singleton, it notmally stays memory resident until program
termination - whereas in PHP, the Singleton is only alive for the
duration of the request?) - unless again, I am missing something.

How about if we take the discussion outside of pure code and into the
real world... I don't know if this will be applicable to a "pure"
singleton pattern as defined in a class. However...

Let's say you have a system where a user logs in, and from that point
you're using sessions to manage the log-in state. Would you allow the
user to say, log in using two or three different web browsers installed
on the same machine? Or (potentially) worse, let them give their log-in
information to friends and co-workers?

Would code that prevents this sort of thing qualify as something that is
attempting a singleton pattern? Say, by checking user_agent, or not
allowing a log-in when the user is already logged in?

Just a bit of wool-gathering, and my 2 cents.

GC
Sep 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would be very interested in knowing how the following C++ multi-instance singleton (AKA Borg) design pattern based code snippet can be neatly coded in Python. While there may be...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
21
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought...
13
by: Robert W. | last post by:
At the beginning of my C# days (about 6 months ago) I learned about the Singleton pattern and implemented for Reference data, such as the kind that appears in an Options dialog box. My Singleton...
14
by: Paul Bromley | last post by:
Forgive my ignorance on this one as I am trying to use a Singleton class. I need to use this to have one instance of my Class running and I think I understand how to do this. My question however is...
2
by: baba | last post by:
Hi all, I'm quite new to C#. I am trying to implement some basics reusable classes using this language and the .NET Framework technology. What I'm trying to do now is to implement a singleton...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
5
by: Lie | last post by:
This is probably unrelated to Python, as this is more about design pattern. I'm asking your comments about this design pattern that is similar in functionality to Singleton and Borg: to share...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.