473,387 Members | 1,904 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,387 software developers and data experts.

Singleton pattern not working

I'm trying to create a singleton (only one instance of a class), but
this doesn't work, can anyone explain this?

<code>

function &get_instance()
{
static $instance;
if (! isset($instance))
{
echo "test";
$instance =& new Foo();
}

return $instance;
}

</code>

It get called like this: $bar =& Foo::get_instance();

I've seen examples on the web, saying this should work. What am I doing
wrong?

--
Henrik S. Hansen
Jul 17 '05 #1
2 2806
On Tue, 11 Nov 2003 23:50:24 +0100, hs*@freecode.dk (Henrik S. Hansen) wrote:
I'm trying to create a singleton (only one instance of a class), but
this doesn't work, can anyone explain this?

<code>

function &get_instance()
{
static $instance;
if (! isset($instance))
{
echo "test";
$instance =& new Foo();
}

return $instance;
}

</code>

It get called like this: $bar =& Foo::get_instance();

I've seen examples on the web, saying this should work. What am I doing
wrong?


It appears to me from the code below that you can't assign by reference to a
static variable? (maybe)

The only difference between Foo::get_instance and Foo::get_instance2 is
missing the & on the assignment to $instance.

Digging a bit further through the manual finds that this is documented, see
the end of:

http://www.php.net/manual/en/languag...bles.scope.php
<pre>
<?php
class Foo
{
function Foo()
{
echo "Foo constructor\n";
}

function &get_instance()
{
static $instance;
var_dump($instance);
if (!isset($instance))
{
echo "test\n";
$instance =& new Foo();
}
var_dump($instance);
return $instance;
}

function &get_instance2()
{
static $instance;
var_dump($instance);
if (!isset($instance))
{
echo "test\n";
$instance = new Foo();
}
var_dump($instance);
return $instance;
}

}

$bar =& Foo::get_instance();
$bar =& Foo::get_instance();
$bar =& Foo::get_instance();

echo "\n";

$bar =& Foo::get_instance2();
$bar =& Foo::get_instance2();
$bar =& Foo::get_instance2();

?>
</pre>

Output:

NULL
test
Foo constructor
object(foo)(0) {
}
NULL
test
Foo constructor
object(foo)(0) {
}
NULL
test
Foo constructor
object(foo)(0) {
}

NULL
test
Foo constructor
object(foo)(0) {
}
object(foo)(0) {
}
object(foo)(0) {
}
object(foo)(0) {
}
object(foo)(0) {
}

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2
Hi !

On Tue, 11 Nov 2003 23:50:24 +0100, hs*@freecode.dk (Henrik S. Hansen)
wrote:
I'm trying to create a singleton (only one instance of a class), but
this doesn't work, can anyone explain this?

<code>

function &get_instance()
{
static $instance;
if (! isset($instance))
{
echo "test";
$instance =& new Foo();
use $instance = new Foo() here. Works for me. }

return $instance;
}

</code>

It get called like this: $bar =& Foo::get_instance();


exactly right.

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

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

Similar topics

1
by: Richard A. DeVenezia | last post by:
foo() generates elements with event handlers that invoke foo function properties. Is this an abhorrent or misthought pattern ? It allows just the one occurence of identifier /foo/ to be changed...
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...
11
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be...
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...
2
by: Chris Murphy via DotNetMonster.com | last post by:
Hey guys, I've been hitting a brick wall with a problem I've come accross in developing an application. Background: The application uses one primary class that I'm trying to implement with the...
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...
2
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates"...
12
by: pythoncurious | last post by:
Hi, I've been trying to get some sort of singleton working in python, but I struggle a bit and I thought I'd ask for advice. The first approach was simply to use a module, and every variable...
4
by: John Sheppard | last post by:
Hello, I am working on a 3teired project, data,bizlogic, userinterface...I have a global class that at current is being passed around as parameters, this is, to say the least messy... I am...
0
by: George Sakkis | last post by:
On Jul 3, 6:58 pm, Urizev <uri...@gmail.comwrote: Because __init__() is called to initialize the state of an object *after* it has already been created. You should create a "new-style" class...
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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.