Comparison between static and instance method in PHP
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()); ?> |
chevron_right
filter_none
Output:
MNNIT Allahabad
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()); ?> |
chevron_right
filter_none
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 // 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 insitialize $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'"; ?> |
chevron_right
filter_none
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'
Recommended Posts:
- Internal static variable vs. External static variable with Examples in C
- PHP | dir() (Get instance of the Directory)
- AWS EC2 Instance Setup with Apache Server
- Class & Instance Attributes in Python
- Java 11 - Features and Comparison
- String comparison using == vs strcmp() in PHP
- AngularJS vs Angular2 | Features and Comparison
- Comparison - Centralized, Decentralized and Distributed Systems
- Static Function in PHP
- When to use static vs instantiated classes in PHP?
- Static vs Dynamic Website
- “static const” vs “#define” vs “enum”
- Static and Dynamic Linking in Operating Systems
- Difference between a Static Queue and a Singly Linked List
- Build a Simple static file web server in Node
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

