I have a little problem understanding the idea behind a singleton. Ok, I understand that the main advantage of a singleton is that there is always one copy of the object of this class (or isn't it?). How is singleton better then normal class?

This question is based on the application I develop with the other fellow. He insists that, for example, database abstraction class would be defined as singleton. He points out that:
1. one instance of a class is faster
2. passing values by reference (as in singleton) is quicker than making a copy
3. he's done some timing and application run faster with singleton version.

Well my replies are:
1. the class is defined and global object is created within the same included file with include_once - so there would be one object anyway, independent of in how many places it is included this way
2. PHP5 passes returned values by reference by default, so having a reference returned by ::getInstance() or a global variable returned by new operator is the same thing, or isn't it?
3. that one I can't check, but it might just as well be a psychological difference 😉

So what do you think, is it worth to change all the classes, whose objects are supposed to be unique, to singletons? Not that it needs a lot of work, but is it worth the fuzz?

    I find singletons are cleaner. All you need to do is manage your class. With globals you need to manage your globals somewhere else.

    In a way I guess Singletons are the OO globals.

      Ok, I understand that the main advantage of a singleton is that there is always one copy of the object of this class (or isn't it?). How is singleton better then normal class?

      You can have multiple instances of a concrete class that is not a singleton. On the other hand, a "normal class" is better than a singleton because... you can have multiple instances of a concrete class that is not a singleton. It really depends on whether you want to enforce a unique object.

      He insists that, for example, database abstraction class would be defined as singleton.

      So, would you ever need more than one instance of that class? What happens if you have more than one otherwise identical instance of that class?

      1. one instance of a class is faster
      3. he's done some timing and application run faster with singleton version.

      That's irrelevant unless speed really is the issue. If there is no significant difference either way, the choice should be made concerning design, not speed.

      2. passing values by reference (as in singleton) is quicker than making a copy

      Singletons and pass by reference are two different concepts. Also, as you pointed out...

      2. PHP5 passes returned values by reference by default, so having a reference returned by ::getInstance() or a global variable returned by new operator is the same thing, or isn't it?

      There is a subtle difference, and it has to do with PHP5 objects being pointers without pointer syntax rather than true references, but often we call them references anyway. But yes, for the purposes of this discussion the efficiency should be the same.

      So what do you think, is it worth to change all the classes, whose objects are supposed to be unique, to singletons?

      Probably.

        It generally makes sense for a Database class to be a singleton, assuming that (1) you never expect to need more than one open database connection at a time, and (2) you only expect to access a single database. If either of those assumptions is false, then you would either not want to use a singleton class, or else you would need to create a separate (child?) singleton class for each connection/database type.

          Indeed. The whole point of a singleton object is if there really is supposed to be only a single instance in the world (or at least in the runtime environment). Particularly if accidentally having two instances could cause Bad Things to happen.

            Thank you all for your replies.
            "Bad Things" will not happen in any of our class if there would accidentally be two objects of any of the classes we have. So I think we'll stay with what we've got now, but keep in mind that for the sake of code "purity" we'll change it some day to singletons 😉

              Write a Reply...