Singleton vs. Static class in PHP

Hi to everyone.

Today we’ll talking about differences between singleton pattern and static class. Both of them uses for access to one resource in application, let say database or some api but this one represented as one exemplar (have the same properties while application is working).

So there are two general aspects – speed and convenience in use…

Speed

I’ve made two tests. The first of them (singleton):

define("TIMES", 1000000);
class Singleton{
	private static $instance;
	private function __construct(){}
	public static function instance(){
		if (!isset(self::$instance))
			self::$instance = new Singleton();
		return self::$instance;
	}
	public function foo(&$param){
		$this->inner_foo($param);
	}
	private function inner_foo(&$param){
		$param = 10 * $param;
	}
}
$t = time();
for ($i = 0; $i < TIMES; $i++) { 	$obj = Singleton::instance(); 	$tmp = 1; 	$tmp = $obj->foo($tmp);
}
echo time() - $t;

And the second (static class):

define("TIMES", 1000000);
class StaticClass{
	public static function foo(&$param){
		self::inner_foo($param);
	}
 
	private static function inner_foo(&$param){
		$param = 10 * $param;
	}
}
$t = time();
for ($i = 0; $i < TIMES; $i++)
{
	$tmp = 1;
	StaticClass::foo($tmp);
}
echo time() - $t;

That do you think who had won in this competition? I ensure that you alredy know answer – of course language construct is more efficient – singleton lose in this aspect. Static class works three times faster.

Convenience

There is interesting article about singletons and static classes in C#. You can find there two advantages of singletons:

Singletons can be used differently than static classes.

This design pattern can be used with interfaces.

Singletons can be used as parameters to methods.

This is only about C#, php is more flexibility in this aspect, just look at:

interface IStaticClass{
	public static function foo(&$param);
}
class StaticClass implements IStaticClass {
	public static function foo(&$param){
		self::inner_foo($param);
	}
 
	private static function inner_foo(&$param){
		$param = 10 * $param;
	}
}
function test_foo($cl) {
	$tmp = 1;
	$cl::foo($tmp);
	echo $tmp;
}
test_foo("StaticClass");

There you can find static class as param (probably not so beautiful) and usage interface with static class. So this is not problem for PHP.

There was only advantages of static classes before but I use singleton because it has `__construct()` function and I can configure singleton as I want also somewhere is accepted to use singleton (for instance in libraties of Kohana framework).

Summary

Static class is more convenient in most cases it’s the best way to store variables, simple functions (in helpers) accessible from everywhere, but if you want make library with config, singleton is better (or probably you just got used to use singleton (: ).

Do you have any other ideas about these handlings?

Books to read

3 Comments

Leave a Reply to win Cancel reply

Your email address will not be published. Required fields are marked *