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.
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.
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.
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'
- Instance method in Python
- Internal static variable vs. External static variable with Examples in C
- PHP | dir() (Get instance of the Directory)
- Class and Instance Attributes in Python
- AWS EC2 Instance Setup with Apache Server
- How to represent the defining instance of a term in Html?
- String comparison using == vs strcmp() in PHP
- Java 11 - Features and Comparison
- AngularJS vs Angular2 | Features and Comparison
- Comparison between GraphQL & RESTful Architecture
- Comparison - Centralized, Decentralized and Distributed Systems
- New self vs. new static in PHP
- Static Function in PHP
- Difference between self::$bar and static::$bar in PHP
- When to use static vs instantiated classes in PHP?
- Static vs Dynamic Website
- What is Late Static Bindings in PHP ?
- How to create static classes in PHP ?
- How to Serve Static Content using Node.js ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.