Professional Web Applications Themes

Singleton pattern not working - PHP Development

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...

  1. #1

    Default 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
    Henrik S. Hansen Guest

  2. #2

    Default Re: Singleton pattern not working

    On Tue, 11 Nov 2003 23:50:24 +0100, [email]hshfreecode.dk[/email] (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 doented, see
    the end of:

    [url]http://www.php.net/manual/en/language.variables.scope.php[/url]


    <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 (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  3. #3

    Default Re: Singleton pattern not working

    Hi !

    On Tue, 11 Nov 2003 23:50:24 +0100, [email]hshfreecode.dk[/email] (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.
    [url]http://sourceforge.net/projects/phpdbedittk/[/url]
    Jochen Daum Guest

Similar Threads

  1. Replies: 42
    Last Post: May 31st, 06:26 PM
  2. When to use a singleton class?
    By Ben Ong in forum ASP.NET Web Services
    Replies: 3
    Last Post: March 8th, 09:46 PM
  3. Replies: 14
    Last Post: December 15th, 02:14 AM
  4. Calling singleton from C
    By Dwayne Achee in forum Ruby
    Replies: 1
    Last Post: November 7th, 09:51 AM
  5. How to do this as a singleton select?
    By Ryan Gaffuri in forum Oracle Server
    Replies: 5
    Last Post: January 9th, 02:33 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139