473,597 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using the "static" keyword to implement the Singleton pattern

To call I would do something like:

$headline = McSelectJustOne Field::callData store("cbHeadli ne");

Is this the correct use of the static keyword, to implement a
Singleton design?

class McSelectJustOne Field extends McSelect {
/**
* 11-21-03 - getter
* @param - $infoToBeSought , in this case, is the name of the field
whose contents are wanted.
* returns mixed (could be string or integer or whatever was in the
field)
function static callDatastore($ infoToBeSought) {
$this->setQueryObject ("GetJustOneFie ld");
$this->setInfoToBeSou ght($infoToBeSo ught);
$this->getInfo();
$row = $this->getRowAsArrayW ithStringIndex( $this->dsResultPointe r);
$field = $row[0];
return $field;
}

}
Jul 17 '05 #1
14 3189
With total disregard for any kind of safety measures
lk******@geocit ies.com (lawrence) leapt forth and uttered:
To call I would do something like:

$headline = McSelectJustOne Field::callData store("cbHeadli ne");

Is this the correct use of the static keyword, to implement a
Singleton design?


Pretty much, regardless of whether you're using the Singleton
pattern, you're still calling a static method. So the 'static'
keyword is perfectly acceptable.

You ARE using PHP5 I assume?

--
There is no signature.....
Jul 17 '05 #2
With total disregard for any kind of safety measures
lk******@geocit ies.com (lawrence) leapt forth and uttered:
To call I would do something like:

$headline = McSelectJustOne Field::callData store("cbHeadli ne");
Is this the correct use of the static keyword, to implement a
Singleton design?


Actually, ignore my previous follow-up. No this is not correct. In
PHP5 the correct usage is:

<?php
class Foo {
public static function aStaticMethod() {
// ...
}
}

Foo::aStaticMet hod();
?>

But this is only available in PHP5
--
There is no signature.....
Jul 17 '05 #3
With total disregard for any kind of safety measures
lk******@geocit ies.com (lawrence) leapt forth and uttered:
To call I would do something like:

$headline = McSelectJustOne Field::callData store("cbHeadli ne");

Is this the correct use of the static keyword, to implement a
Singleton design?


P.P.S: (sorry, I'm a dumbass and my server doesn't refresh quick
enough), the variable $this does not exist within static methods.

--
There is no signature.....
Jul 17 '05 #4
Phil Roberts <ph*****@HOLYfl atnetSHIT.net> wrote in message news:<Xn******* *************** *@206.127.4.22> ...
With total disregard for any kind of safety measures
lk******@geocit ies.com (lawrence) leapt forth and uttered:
To call I would do something like:

$headline = McSelectJustOne Field::callData store("cbHeadli ne");

Is this the correct use of the static keyword, to implement a
Singleton design?


Pretty much, regardless of whether you're using the Singleton
pattern, you're still calling a static method. So the 'static'
keyword is perfectly acceptable.

You ARE using PHP5 I assume?

No, sorry, I'm not using PHP 5. I'm trying to make this work in PHP 4.
Jul 17 '05 #5
Phil Roberts <ph*****@HOLYfl atnetSHIT.net> wrote in message news:<Xn******* *************** *@206.127.4.22> ...
With total disregard for any kind of safety measures
lk******@geocit ies.com (lawrence) leapt forth and uttered:
To call I would do something like:

$headline = McSelectJustOne Field::callData store("cbHeadli ne");

Is this the correct use of the static keyword, to implement a
Singleton design?


P.P.S: (sorry, I'm a dumbass and my server doesn't refresh quick
enough), the variable $this does not exist within static methods.

Actually, I'm using the static keyword all wrong. I looked up an old
example that I had almost forgotten. Thank god for Google. This is
what someone suggested to me 8 months ago. I didn't understand it at
the time, but now that I look at it, I realize static is being used
here in a clever way, to get around the limitations of PHP 4.
All this data should only reference one single object. Having only one Object (called Singleton Pattern) can be done by a Mediator (Pattern).
A mediator looks basically like this:

class mediator{

function &instance(){

static $instance;
if (!isset($instan ce)){
$instance = new object;
}

return $instance;
}
}

Which should be referenced absolutely everywhere by

$ins =& mediator::insta nce();

You will then have exactly one object always. Also, you can get rid of all global statements with this.


Brilliant.

Jul 17 '05 #6
Hi !

(...)

Actually, I'm using the static keyword all wrong. I looked up an old
example that I had almost forgotten. Thank god for Google. This is
what someone suggested to me 8 months ago.


wasn't that long ago.

Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #7
Hi...

lawrence wrote:
Is this the correct use of the static keyword, to implement a
Singleton design?


Why do you need a singleton? Singletons are only a short step up from
globals in causing trouble. They are a right royal pain when testing as
one test can influence another. I wrote an article on the PHP Patterns
site (http://www.phppatterns.com/) on how to get around this issue, and
it involves some serious extra work.

Can you do without it or are you just trying it out as a learning exercise?

yours, Marcus
--
Marcus Baker, ma****@lastcraf t.com, no***@appo.demo n.co.uk

Jul 17 '05 #8
Marcus Baker wrote:
Singletons are only a short step up from globals in causing trouble.
Singletons IMHO avoid the major drawbacks of global objects (which is
the motivation for the pattern if I remember right). They initialize
themselves when first used and they can't be deleted or overwritten by
accident.
They are a right royal pain when testing as one test can influence another.


That depends on the design of your classes and your tests. I usually use
a Factory Method in the client object to get the singleton instance. For
testing I swap that Factory Method out against one that returns a mock
of the Singleton. (I use your Partial Mocks for that.)

Having Singletons carry data that changes after initialization to
reflect the state of the application is indeed problematic, because it
leeds to temporal coupling, which is near impossible to test well. I use
Singletons mainly for "stateless" things that don't change after
initialization. A database connection is a prime example.

Jochen

Jul 17 '05 #9
Marcus Baker <ma****@lastcra ft.com> wrote in message news:<3F******* *******@lastcra ft.com>...
Hi...

lawrence wrote:
Is this the correct use of the static keyword, to implement a
Singleton design?


Why do you need a singleton? Singletons are only a short step up from
globals in causing trouble. They are a right royal pain when testing as
one test can influence another. I wrote an article on the PHP Patterns
site (http://www.phppatterns.com/) on how to get around this issue, and
it involves some serious extra work.


Can you give a more specific link? Your article is no longer on the
front page. I'd like to read it over.
Jul 17 '05 #10

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

Similar topics

29
4376
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
3
1704
by: Zeljko | last post by:
Hi, Is there a way to define a local variable that retains it's value between the function calls ? Zeljko
9
2298
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts that the app uses. So I will need to change the fonts depending on what settings the user has entered. However, it seems kind of wasteful to me to go to teh registry, fetch the font information and create new font objects for every form that I am...
11
1907
by: Dave | last post by:
I'm trying to understand the implications of using static methods and properties in asp.net so I found an article "Troubleshooting ASP.NET applications with the use of static keywords" (http://support.microsoft.com/?id=893666) that discusses the possiblity of users seeing other users data if they access static methods at the same time because values are shared. Although I never used it, I noticed that some SqlHelper functions part of...
11
2135
by: comp.lang.php | last post by:
function blah($item) { if (!isset($baseDir)) { static $baseDir = ''; $baseDir = $item; print_r("baseDir = $baseDir\n"); } $dirID = opendir($item); while (($fyl = readdir($dirID)) !== false) { if (is_dir("$baseDir/$fyl")) blah($item);
3
1566
by: Asfand Yar Qazi | last post by:
For years, I've been putting everything that won't be needed outside a compilation unit in anonymous namespaces, even editing my old files that did things the 'old' way (using static linkage). Then suddenly I find this in the KDE developer faq: Q: 2.24. I put some functions in anonymous namespace and someone reverted it and made those functions static, why? Symbols defined in a C++ anonymous namespace do NOT have internal linkage....
5
3839
by: chandu | last post by:
can any one give advantages and disadvantages to using static methods in a class. shall we use complete static methods in a class ? thanks
14
5997
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
7
1738
by: Anna Smidt | last post by:
I have a function which will return a shape from an image everytime it's called. Since it's time consuming I would like it to run itself only once, and at every subsequent calls it should return the shape it calculated the first time. In VB6 I would have used the key word "static" to preserve the results from the first call. Is there an equivalent in C++? --------------- In VB6 I would have simply said
0
7962
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8267
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8258
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6681
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5844
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2394
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 we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1229
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.