Static vs Non-static methods in PHP. what is faster?

As you know there are two types of methods in PHP classes: static and non-static. To be called a non-static method needs an instance of its class, and static method can be called without instantiating of the class. 

In the meantime there are two very common questions:

  • When to use non-static methods and when to use static methods?
  • Is there any performance difference between static and non-static methods?

These are very common dilemmas among PHP developers. Most of developers guess that static methods must work faster because there is no object instance with all its properties involved to the call. Sounds logical. Is that actually true?

As I already mentioned above, and as you already know, the main difference between static and non-static methods is that static methods do not need an instance of the class to be called in the code. It means that for static methods there is no need to create object and keep in the memory. So, at least we can save some memory which can also affect the performance

Some experiments

To test the performance of static and nonstatic methods let’s create two similar classes. The only difference will be how we call methods in the class.

 

Static class

<?php
set_time_limit(0);
Class Static0 {

	public static $counter = 0;
	
	public static function incrByTwo()
	{
		self::$counter = self::$counter + 2;
	}
}

Class Static1 extends Static0 {

	public static function incrByOne()
	{
		self::$counter = self::$counter + 1;
	}
	
	public static function incrByTwo()
	{
		parent::incrByTwo();
	}
}

$time_start = microtime(true);
for($i=0;$i<100000;$i++) {
	Static1::incrByOne();
	Static1::incrByTwo();
}

echo Static0::$counter ;
$time_end = microtime(true);
$time1 = $time_end - $time_start;
?>
Total execution time is <?php echo $time1;?>
Used memory: <?php echo memory_get_usage();?>

NonStatic class

<?php
set_time_limit(0);

Class Static0 {

	public $counter = 0;

	public function incrByTwo()
	{
		$this->counter = $this->counter + 2;
	}
}

Class Static1 extends Static0 {
	
	public function incrByOne()
	{
		$this->counter = $this->counter + 1;
	}
	
	public function incrByTwo()
	{
		parent::incrByTwo();
	}
}

$time_start = microtime(true);
$obj = new Static1();
for($i=0;$i<100000;$i++) {
	$obj->incrByOne();
	$obj->incrByTwo();
}

echo $obj->counter ;

$time_end = microtime(true);
$time1 = $time_end - $time_start;
?>

Total execution time is <?php echo $time1;?>
Used memory: <?php echo memory_get_usage();?>

Okay, now we know that static methods are indeed generally faster than non-static methods. And the difference in memory consumption doesn’t look significant at all…

So, use static methods everywhere now?

Not really. Short answer to this question would be: you don’t need an object and don’t need a non-static method if the method doesn’t interact with properties or methods in the class and its parents/children.

Yes, static methods look  faster. In the meantime, a lot of developers believe (and they have good reasons) that using static methods and properties break a lot of the power available to Object-Oriented code. They also mention that Unit testing is impossible using static methods.