Comparison between static and instance method in PHP Last Updated : 18 Feb, 2022 Comments Improve Suggest changes Like Article Like Report 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 // 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 <?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: The static method can call without object while instance method can not be called without object.The execution time of instance method is faster than static method but in the PHP version 5.3 there was a bug which says that static methods are faster in which they have introduced late binding. Example: php <?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' Comment More infoAdvertise with us Next Article Comparison between static and instance method in PHP A ankit15697 Follow Improve Article Tags : Technical Scripter Web Technologies PHP PHP Programs Technical Scripter 2018 +1 More Similar Reads What is the difference between is_a() function and instanceof in PHP? is_a() Function The is_a() is a built-in function in PHP which is used to check whether a given object is of a given class or not. It also checks if the given class is one of the parents of the given object or not. Syntax: bool is_a( $object, $class_name, $allow_string ) Parameters: This function ac 3 min read What are getters and setters methods in PHP ? In object-oriented programming, getters and setters are methods used to access and modify the private or protected properties of a class. These methods provide a controlled way to retrieve and update the values of class properties, promoting encapsulation and maintaining the integrity of an object's 3 min read Why does PHP 5.2+ disallow abstract static class methods ? Before we answer this question, you must have a clear definition of what exactly is an abstract class, an abstract method and a static method. Abstract Class: In the Object-Oriented programming paradigm, abstraction refers to the process of hiding the internal implementation details of any program a 2 min read Difference between self::$bar and static::$bar in PHP self keyword: It is a PHP keyword that represents the current class and used to access static class variables or static variables because these members belong to a class rather than the object of that class. Example: php <?php // Declaring parent class class demo { public static $bar = 10; public 2 min read What is Late Static Bindings in PHP ? In PHP, programs are saved and then directly run on the browser, the script is executed through a web server and we get the output. We don't compile PHP programs manually but that does not mean it is never compiled. The PHP interpreter does that for you and runs it. So there are two phases, first, c 4 min read Like