Singleton vs Abstract + Static

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
stukov
Forum Commoner
Posts: 26
Joined: Sun Jul 24, 2005 2:16 pm
Location: Sherbrooke, Qc, Canada

Singleton vs Abstract + Static

Post by stukov »

What is the main difference between a singleton and an abstract class with only static methods and functions? I mean, both of these will only get one "instance".

Is there any performance difference?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

There is significant difference.

Possible instances of an abstract class: 0
Possible instances of a singleton class: 1


An abstract class is abstract, and can't be instantiated per definition.
stukov
Forum Commoner
Posts: 26
Joined: Sun Jul 24, 2005 2:16 pm
Location: Sherbrooke, Qc, Canada

Post by stukov »

Yes, I know that theorically, you can't have an instance of the abstract class, but while using it with static methods and functions, it appears to be "like" an "instance" of the class itself. May these line of code explain. Let's consider:

Code: Select all

class one {
var $foo;

  function bar() {
    $this->foo = 4;
  }
}
and

Code: Select all

abstract class Singleton {
  static $foo;
    
  function bar() {
    self::$foo = 4;
  }
}
In both cases you will be able to print out the var $foo and you will get "4". Is one of these ways to design a "singleton" is better than an other (let's say in an extreme environnement - 100 000 visits / minute).
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Is one of these ways to design a "singleton" is better than an other.
Well, if you are going to implement a singleton, it would make sense to have code that allows you to have a single instance...

Here are some alternatives to implement a singleton: http://www.tonymarston.net/php-mysql/singleton.html.

You are allowed to do some benchmarks on the implementations and let us know what the outcome was :)
stukov
Forum Commoner
Posts: 26
Joined: Sun Jul 24, 2005 2:16 pm
Location: Sherbrooke, Qc, Canada

Post by stukov »

timvw: thanks for the link, it was very helpful.
timvw wrote:You are allowed to do some benchmarks on the implementations and let us know what the outcome was :)
All right, I'll try this tonight. I'll be back in a while with the results :D.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Oh God the dreaded Tony Marston... Steer well clear unless you want to be set back several years in your understanding of OOP.

Some better reading material: http://www.phppatterns.com/index.php/ar ... ew/75/1/1/.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Why don't you just (and this is PHP4) stick the singleton in a global variable and take care not to call it again?
Post Reply