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'
- Internal static variable vs. External static variable with Examples in C
- PHP | dir() (Get instance of the Directory)
- Class & Instance Attributes in Python
- AWS EC2 Instance Setup with Apache Server
- 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
- Static vs Dynamic Website
- When to use static vs instantiated classes in PHP?
- “static const” vs “#define” vs “enum”
- Static and Dynamic Linking in Operating Systems
- Bound, unbound, and static methods in Python
- 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 firstname.lastname@example.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.