SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36
  1. #26
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ryan Wray
    Would a term more like Writable Singleton make more sense than Multipleton. It isn't holding multiple instance, but rather you can change the instance the singleton is holding and discard the old one (in other words, the Singleton is no longer read-only).

    A Registry simply makes more sense to me, though I know you have dimissed it.
    Eh? I thought a singleton was writable anyway as the whole point of a singleton was to allow global access to a single instance of an object?

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  2. #27
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by OfficeOfTheLaw
    Eh? I thought a singleton was writable anyway as the whole point of a singleton was to allow global access to a single instance of an object?
    Well, every time I have seen a singleton (in PHP) it has always been pretty much a hard-coded object initialation stored in a static variable. For instance, a simply one (function)

    PHP Code:
    <?php
    function getDatabaseConnection()
    {
        static 
    $instance;
        
        if (!isset(
    $instance)) {
            
    $instance = new Database;
        }
        return 
    $instance;
    }
    ?>
    A class singleton isn't a whole lot different. Now, granted, you could add a mutator I don't know if anyone uses singletons in such a way.

    Anyhow, it seems to me he simply wants a Singleton (ie. only have one instance at a time available, globally) that can have the instance in question changed. Maybe I just don't understand what he doing.

  3. #28
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No matter how you slice it, you can still call functions statically in PHP4, and as such something like this doesn't give you anything that just calling said functions statically doesn't do, aside from some debatably useful CS purity (because you couldn't modify the variable pointing to the most recently created instance as easily).

    Of course, any argument that way is shot in the foot by PHP4's most glaring flaw--namely in that objects are more or less just arrays, and I can change the value of any given element at any time I want. Singletons (and their variants, like what you're trying to do here) exist for the sole purpose of making sure that there's only one instance of some particular item, because if there were more than one, it would cause problems.

    Generally speaking, Singletons have limited usefulness in this regard, as a class that actually has problems when there is more than one around is either badly programmed, or hardware-limited (semaphores, hardware drivers, and the like are candidates for this sort of thing, although it's extremely rare to encounter those in OOP anyway).

    PHP singletons are slightly more useful because they can be used as a hack to solve the lack of namespaces. (Class::Function() instead of Class_Function or ClassFunction or whatever).

    Ultimately, the question is this: What tangible, useful advantage would a construct like this actually add to your code? What situations would you encounter where this would be useful in real life?

  4. #29
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> Singletons (and their variants, like what you're trying to do here) exist for the sole purpose of making sure that there's only one instance of some particular item

    They also exist to make that single instance "global" without resorting to $GLOBAL['object'] or global $object.

  5. #30
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> A Registry simply makes more sense to me

    >> Sorry, I read the thread, but don't really get what you are meaning to do that isn't done by the registry pattern

    As I've said several times in this thread, I believe a Registry is a much better approach when one is using multiple objects. Just as I believe a "Singleton Registry" is better than having multiple Singletons.

    However, IMO a Registry is overkill for cases of just a single object that can carry multiple instances (dropping the old one when a new one is created), just as IMO, it would be overkill for a case where one needs a single object with a single instance. A singleton is better suited in the latter case, IMO, unless one just likes to needlessly wrap their classes around other classes just because it makes them feel smarter.

    As I see it:
    - single object, single instance = singleton
    - multiple objects, single instance of each = "singleton registry"
    - multiple objects, single or multiple instances = registry
    - single object, multiple instances = .... nothing?

    This is what I came up with to fill the void in a case where I needed it, and nothing already exits to fill it (a registry makes it overflow). That is all it is and nothing more.

  6. #31
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> What tangible, useful advantage would a construct like this actually add to your code?

    That is up for you to decide.

    >> What situations would you encounter where this would be useful in real life?

    I have already described several times the "real life" situation for why I created this. I did not come up with it just for the sake of coming up with it. Whether or not it is useful to you or anyone else, that is up to you and you alone to decide. Some things we have to walk on our own.

  7. #32
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> Would a term more like Writable Singleton make more sense than Multipleton. It isn't holding multiple instance, but rather you can change the instance the singleton is holding and discard the old one (in other words, the Singleton is no longer read-only).

    Yes precisely. That is exactly what I needed, and why I came up with this. Finally someone "gets it"... you seem to be one of the few to have actually read the thread. (sorry, but it just gets damn annoying when people continually ask "why not use a registry?", a question to which I have answered several times now).

    Singletons limit objects to one instance (once created you cannot change it). Registries are for multiple objects (IMO). I needed a single object whose instance could be changed, and whose current instance could always be returned (to be "global"). Singleton was too limited. Registry was too much. So I did this. Never claimed it would useful to anyone else or to any other circumstances though...

  8. #33
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always thought this was trivial.
    PHP Code:
    class Multipleton
    {
        private static 
    $instance NULL;
        protected function 
    __construct($param) {/*...*/}
        
        public static function 
    instance() { return self::$instance; }
        public static function new(
    $param)
        {
            
    self::$instance = new self($param);
            return 
    self::$instance;
        }

    And it's essentially the same in PHP4. What's the fuss all about?

    BTW and IMO, a Singleton that can hold many instances of itself has/is/uses a singleton registry. It's just that this one only holds one at a time - but I don't think that necessarily makes it anything more than a Singleton.

  9. #34
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> And it's essentially the same in PHP4. What's the fuss all about?

    Yeah that is how one could do it in PHP 5. As I've said a million times already, like everything else I've said since people can't read I guess, it is trivial and far easier in PHP 5. But PHP 4 doesn't support static class properties, so it becomes trickier to do the same essential thing.

  10. #35
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, having a hard time reading your thoughts.

  11. #36
    SitePoint Zealot Rotwang's Avatar
    Join Date
    Jun 2005
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Helge
    There is no need to attacking each other. Discuss in a civilized manner and this thread won't be closed. This is the only warning you people get.
    This is a crappy warning. "You two stop fighting" always short-changes one of the two. Plus they're sticking to technical talk so what's the problem.


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
  •