Open In App

Comparison between static and instance method in PHP

Static methods 
The static method in PHP is same as other OOP languages. Static method should be used only when particular data remains constant for the whole class. As an example, consider that some programmer is making the data of a college and in that every object needs getCollegeName function that returns the same college name for all objects as name of college then this function should be made static. Basically static methods are used when to access that method without the object of that class. Static method are used when there is a chance to use method without help of class objects.
Example: 
 




<?php
 
// PHP code for static method
class College
{
    // Static function
    static function getCollegeName()
    {
            return "MNNIT Allahabad";
    }
}
 
// Calling function without its object
echo (College::getCollegeName());
?>

Output: 

MNNIT Allahabad

 

Instance Methods 
Instance method is used when there is no chance to call the method without existing of object. For example consider a College class in which getPersonName() method which returns the name of person. This method should exist only when there is a particular type of person object.
Example: 
 




<?php
// Program for instance methods
 
class College
{
    // This data can not be accessed
    // from outside of the class
    private $name;
     
    // This function sets the name
    function setName($name) {
        $this -> name = $name;
    }
     
    // This function return name
    function getName() {
        return $this -> name;
    }
}
 
// Creating an object of type College
$obj = new College;
 
// Setting name
$obj -> setName("Geeks");
 
// Getting name
echo ($obj -> getName());
?>

Output: 

Geeks

 

Comparison between instance and static methods: 
 

Example: 
 




<?php
 
// Program to compare execution time
// of static and instance methods
set_time_limit(0);
 
// Checking php version
echo 'Current PHP version: ' . phpversion();
 
// Creating class for static
Class St {
 
    public static $count = 0;
     
    // function that will print nothing
    public static function getResult()
    {
        self::$count = self::$count + 1;
    }
}
 
// For calculating time
$time_start_static = microtime(true);
 
// This loop will run 100000 times
for($i = 0; $i < 100000; $i++) {
    St::getResult();
}
 
// Execution time for static method ends here
$time_end_static = microtime(true);
 
// Execution time is end -start time
$time_static = $time_end_static - $time_start_static;
 
// Printing the result
echo "\nTotal execution time is for static method is: '$time_static'";
 
// Demo class for instance method
class Ins {
    private $count = 0;
     
    // Function that will not print anything
    public function getResult() {
        $this -> count = $this -> count + 2;
    }
}
 
// Creating an instance object
$ob = new Ins;
 
// Starting time is initialize
$time_start_instance = microtime(true);
 
// For time loop is run
for($i = 0; $i < 100000; $i++)
{
    $ob -> getResult();
}
 
// Execution end for instance method
$time_end_instance = microtime(true);
 
// Total time is end-start time
$time_instance = $time_end_instance - $time_start_instance;
 
// Result is printed
echo "\nTotal execution time for instance method is: '$time_instance'";
?>

Output: 
Current PHP version: 7.0.32-0ubuntu0.16.04.1
Total execution time is for static method is: '0.0056149959564209'
Total execution time for instance method is: '0.004885196685791'

 


Article Tags :