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
class College
{
static function getCollegeName()
{
return "MNNIT Allahabad";
}
}
echo (College::getCollegeName());
?>
|
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
class College
{
private $name ;
function setName( $name ) {
$this -> name = $name ;
}
function getName() {
return $this -> name;
}
}
$obj = new College;
$obj -> setName("Geeks");
echo ( $obj -> getName());
?>
|
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
set_time_limit(0);
echo 'Current PHP version: ' . phpversion();
Class St {
public static $count = 0;
public static function getResult()
{
self:: $count = self:: $count + 1;
}
}
$time_start_static = microtime(true);
for ( $i = 0; $i < 100000; $i ++) {
St::getResult();
}
$time_end_static = microtime(true);
$time_static = $time_end_static - $time_start_static ;
echo "\nTotal execution time is for static method is: '$time_static' ";
class Ins {
private $count = 0;
public function getResult() {
$this -> count = $this -> count + 2;
}
}
$ob = new Ins;
$time_start_instance = microtime(true);
for ( $i = 0; $i < 100000; $i ++)
{
$ob -> getResult();
}
$time_end_instance = microtime(true);
$time_instance = $time_end_instance - $time_start_instance ;
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'
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Feb, 2022
Like Article
Save Article