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?
Singleton vs Abstract + Static
Moderator: General Moderators
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:
and
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).
Code: Select all
class one {
var $foo;
function bar() {
$this->foo = 4;
}
}Code: Select all
abstract class Singleton {
static $foo;
function bar() {
self::$foo = 4;
}
}Well, if you are going to implement a singleton, it would make sense to have code that allows you to have a single instance...Is one of these ways to design a "singleton" is better than an other.
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
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/.
Some better reading material: http://www.phppatterns.com/index.php/ar ... ew/75/1/1/.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US