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.
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'