using the "static" keyword to implement the Singleton pattern

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence

    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;
    }

    }
  • Phil Roberts

    #2
    Re: using the "static&qu ot; keyword to implement the Singleton pattern

    With total disregard for any kind of safety measures
    lkrubner@geocit ies.com (lawrence) leapt forth and uttered:
    [color=blue]
    > 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?
    >[/color]

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

    Comment

    • Phil Roberts

      #3
      Re: using the "static&qu ot; keyword to implement the Singleton pattern

      With total disregard for any kind of safety measures
      lkrubner@geocit ies.com (lawrence) leapt forth and uttered:
      [color=blue]
      > 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?
      >[/color]

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

      Comment

      • Phil Roberts

        #4
        Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

        With total disregard for any kind of safety measures
        lkrubner@geocit ies.com (lawrence) leapt forth and uttered:
        [color=blue]
        > 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?
        >[/color]

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

        Comment

        • lawrence

          #5
          Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

          Phil Roberts <philrob@HOLYfl atnetSHIT.net> wrote in message news:<Xns943CD2 07FECphilrobert s@206.127.4.22> ...[color=blue]
          > With total disregard for any kind of safety measures
          > lkrubner@geocit ies.com (lawrence) leapt forth and uttered:
          >[color=green]
          > > 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?
          > >[/color]
          >
          > 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?[/color]


          No, sorry, I'm not using PHP 5. I'm trying to make this work in PHP 4.

          Comment

          • lawrence

            #6
            Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

            Phil Roberts <philrob@HOLYfl atnetSHIT.net> wrote in message news:<Xns943C17 36FFBphilrobert s@206.127.4.22> ...[color=blue]
            > With total disregard for any kind of safety measures
            > lkrubner@geocit ies.com (lawrence) leapt forth and uttered:
            >[color=green]
            > > 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?
            > >[/color]
            >
            > 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.[/color]


            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.


            [color=blue][color=green]
            >> All this data should only reference one single object. Having only[/color][/color]
            one[color=blue][color=green]
            >> Object (called Singleton Pattern) can be done by a Mediator[/color][/color]
            (Pattern).[color=blue][color=green]
            >>
            >> 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[/color][/color]
            of[color=blue][color=green]
            >> all global statements with this.[/color]
            >
            >Brilliant.[/color]

            Comment

            • Jochen Daum

              #7
              Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

              Hi !

              (...)[color=blue]
              >
              >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.[/color]

              wasn't that long ago.

              Jochen
              --
              Jochen Daum - CANS Ltd.
              PHP DB Edit Toolkit -- PHP scripts for building
              database editing interfaces.
              Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

              Comment

              • Marcus Baker

                #8
                Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                Hi...

                lawrence wrote:[color=blue]
                > Is this the correct use of the static keyword, to implement a
                > Singleton design?[/color]

                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, marcus@lastcraf t.com, nokia@appo.demo n.co.uk

                Comment

                • Jochen Buennagel

                  #9
                  Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                  Marcus Baker wrote:
                  [color=blue]
                  > Singletons are only a short step up from globals in causing trouble.[/color]

                  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.
                  [color=blue]
                  > They are a right royal pain when testing as one test can influence another.[/color]

                  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

                  Comment

                  • lawrence

                    #10
                    Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                    Marcus Baker <marcus@lastcra ft.com> wrote in message news:<3FDB3601. 5090908@lastcra ft.com>...[color=blue]
                    > Hi...
                    >
                    > lawrence wrote:[color=green]
                    > > Is this the correct use of the static keyword, to implement a
                    > > Singleton design?[/color]
                    >
                    > 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.[/color]

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

                    Comment

                    • lawrence

                      #11
                      Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                      Marcus Baker <marcus@lastcra ft.com> wrote in message news:<3FDB3601. 5090908@lastcra ft.com>...[color=blue]
                      > Hi...
                      >
                      > lawrence wrote:[color=green]
                      > > Is this the correct use of the static keyword, to implement a
                      > > Singleton design?[/color]
                      >
                      > 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.[/color]

                      What I wanted to do was this:

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

                      I want this mostly for convenience. I do realize it is a bit like
                      having a global function. I'm aware some people criticize this kind of
                      thing in Java for being "not really OO". Damned convenient though.

                      Point me to your article so I can get some sense of the downside.

                      Comment

                      • Jochen Buennagel

                        #12
                        Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                        lawrence wrote:[color=blue]
                        > What I wanted to do was this:
                        >
                        > $headline = McSelectJustOne Field::callData store("cbHeadli ne");[/color]

                        This doesn't really look like a singleton to me, but rather like a class
                        with static methods. But then again I don't know what
                        McSelectJustOne Field looks like inside. Does callDatastore() return a
                        reference to the singleton object? If it does what it looks like (return
                        a string?) that's not a singleton by my book...
                        [color=blue]
                        > Point me to your article so I can get some sense of the downside.[/color]

                        The article is on phpclasses.com in the "Design" section, entitled
                        "Singleton Registry". There's also an article about the Singleton
                        pattern by Harry way down on the page. Read that one first to find out
                        if what you're doing is really a singleton.

                        Jochen

                        Comment

                        • Marcus Baker

                          #13
                          Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                          Hi...

                          Jochen Buennagel wrote:[color=blue]
                          > 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.[/color]

                          Well I wouldn't go so far as to say they are the end of the world, and
                          they do solve the initialisation problem. I even have a couple in my
                          current main app (don't tell anybody). The trouble is that they can get
                          damaged by any part of the code. Also if you ship some with a library,
                          because of the global scope they can affect any part of that library.
                          This makes understanding things just a little bit harder. It's little
                          bit less encapsulation.
                          [color=blue]
                          > 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.)[/color]

                          A factory is almost always a superior approach. This is not so much a
                          singleton though, as you have to get access to the holding class to call
                          the factory method and, as you say, that can be mocked or switched by
                          subclassing. I am not against a cached instance. That is just normal
                          coding. It's the static global access and single instance in combination.

                          Actually I am going off of static methods as well, but that's another
                          story :).
                          [color=blue]
                          >
                          > 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.[/color]

                          That is a common one; PEAR uses it for example. In my current main job
                          (Wordtracker) we have one for the SessionPool (for PHP $_SESSION handler
                          access) and a Registry class (not surprisingly). That is just under 1%
                          of the total number of classes. When I see half a dozen in an
                          application I see it as a symptom of possible problems.

                          One that has caused me particular problems in the past is some kind of
                          Singleton "Auth" class. It often means you have to log in just to run
                          your tests. Given that such a class exists pretty near the top of the
                          application hierarchy (unlike a database connection) there really is no
                          excuse for not passing the object as a parameter.
                          [color=blue]
                          >
                          > Jochen[/color]

                          yours, Marcus

                          p.s. I'll try to do some PHP::Duploc work this holiday. Not finding much
                          time right now.
                          --
                          Marcus Baker, marcus@lastcraf t.com, nokia@appo.demo n.co.uk

                          Comment

                          • Jochen Bünnagel

                            #14
                            Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                            Marcus Baker wrote:[color=blue]
                            > Jochen Buennagel wrote:[color=green]
                            >> 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.)[/color]
                            >
                            > A factory is almost always a superior approach. This is not so much a
                            > singleton though, as you have to get access to the holding class to call
                            > the factory method and, as you say, that can be mocked or switched by
                            > subclassing.[/color]

                            No, I didn't mean a factory class that returns the singleton. The
                            factory method in the client object is usually private, and fetches the
                            singleton only for internal use by the class. If I don't discard it
                            right after use, the member variable that holds it is privat too.
                            [color=blue]
                            > One that has caused me particular problems in the past is some kind of
                            > Singleton "Auth" class. It often means you have to log in just to run
                            > your tests. Given that such a class exists pretty near the top of the
                            > application hierarchy (unlike a database connection) there really is no
                            > excuse for not passing the object as a parameter.[/color]

                            Funny: We have a Auth object that gets created on every page and passed
                            into every object, even though there are circumstances where it will not
                            be used at all. I'm now trying to convince my team that we should turn
                            it into a singleton, so it will be initialized only when actually
                            needed. (Auth is expensive, as our application authenticates against a
                            remote SOAP service.)

                            Jochen

                            Comment

                            • Marcus Baker

                              #15
                              Re: using the &quot;static&qu ot; keyword to implement the Singleton pattern

                              Hi Jochen.

                              Jochen Bünnagel wrote:[color=blue]
                              > No, I didn't mean a factory class that returns the singleton. The
                              > factory method in the client object is usually private, and fetches the
                              > singleton only for internal use by the class. If I don't discard it
                              > right after use, the member variable that holds it is privat too.[/color]

                              No I meant factory in that sense too. To me a Singleton is a single
                              instance with global access. If you can make the access non-global, you
                              are in the clear in my book. I would call what you are doing as caching.
                              [color=blue]
                              > Funny: We have a Auth object that gets created on every page and passed
                              > into every object, even though there are circumstances where it will not
                              > be used at all. I'm now trying to convince my team that we should turn
                              > it into a singleton, so it will be initialized only when actually
                              > needed. (Auth is expensive, as our application authenticates against a
                              > remote SOAP service.)[/color]

                              Sods law you would be doing just that :).

                              If efficiency is a concern you could either lazy proxy the class or the
                              resources in it. The most highly factored approach would be to have a
                              separate Authoriser and Authorisation. The Authoriser::log in method is a
                              factory for the Authorisation here. I am a big fan of factory methods
                              and factory classes if you haven't already guessed.

                              I read somewhere that it is advisable to separate authentication
                              (identifying the user) from authorisation (handing them a set of
                              permissions), but I haven't yet needed that level of sophistication.
                              [color=blue]
                              >
                              > Jochen
                              >[/color]

                              yours, Marcus
                              --
                              Marcus Baker, marcus@lastcraf t.com, nokia@appo.demo n.co.uk

                              Comment

                              Working...