Comparison between Static and Instance Method in PHP


What is Static Method?

In PHP, a static method is a method that belongs to the class itself rather than an instance (object) of the class. It can be accessed directly using the class name, without the need to create an instance of the class.

Example

<?php
class MathUtility {
   public static function square($number) {
      return $number * $number;
   }

   public static function cube($number) {
      return $number * $number * $number;
   }
}

// Calling the static methods without creating an instance
$squared = MathUtility::square(5);
$cubed = MathUtility::cube(3);

echo $squared . "<br>"; // Output: 25
echo $cubed; // Output: 27

?>

Output

25
27

In the example above, we have a MathUtility class with two static methods: square() and cube(). These methods perform simple mathematical operations and return the result. Since the methods are static, we can directly access them using the class name followed by the :: operator, without creating an instance of the MathUtility class.

What is Instance Method?

In PHP, an instance method is a method that belongs to an instance (object) of a class. It can only be accessed through an instance of the class, rather than directly using the class name.

Example

<?php
class Circle {
   private $radius;

   public function __construct($radius) {
      $this->radius = $radius;
   }

   public function calculateArea() {
      return pi() * $this->radius * $this->radius;
   }

   public function calculateCircumference() {
      return 2 * pi() * $this->radius;
   }
}

// Creating an instance of the Circle class
$circle = new Circle(5);

// Accessing instance methods through the object
echo $circle->calculateArea()."<br>"; // Output: 78.539816339745
echo $circle->calculateCircumference(); // Output: 31.41592653589793
?>

Output

78.539816339745
31.415926535898

In the example above, we have a Circle class with three methods: a constructor (__construct()), and two instance methods (calculateArea() and calculateCircumference()). The instance methods perform calculations based on the radius of the circle, which is an instance-specific property ($radius).

To use the instance methods, we first create an instance of the Circle class by calling the constructor and passing the radius value. Then, we can access the instance methods using the object name (in this case, $circle) followed by the -> operator.

Comparison between Static and Instance Method in PHP

Example

<?php
class MathOperations {
   public static function multiply($a, $b) {
      return $a * $b;
   }

   public function add($a, $b) {
      return $a + $b;
   }
}

// Static method example
echo MathOperations::multiply(5, 2) ."<br>";

// Instance method example
$obj = new MathOperations();
echo $obj->add(3, 4); 
?>

Output

10
7

Explanation of code

In the code above, we have a MathOperations class with a static method multiply() and an instance method add(). Let's compare them:

Static Method

  • We directly call the static method multiply() using the class name (MathOperations::multiply(5, 2)).

  • The static method multiply() performs the multiplication operation and returns the result.

  • In the example, we call multiply(5, 2), which gives us the product of 10.

  • Static methods can be accessed and used without creating an instance of the class.

Instance Method

  • We create an instance of the MathOperations class using $obj = new MathOperations().

  • We call the instance method add() using the object name ($obj->add(3, 4)).

  • The instance method add() performs the addition operation and returns the result.

  • In the example, we call add(3, 4), which gives us the sum of 7.

  • Instance methods are invoked on an instance (object) of the class using the object name and the -> operator.

Conclusion

In Conclusion, Static methods in PHP are accessed and used directly through the class name, without requiring an instance of the class. They are useful for utility functions or operations that don't rely on specific instance data. Static methods can be accessed from anywhere in the code and are not tied to any particular object. On the other hand, instance methods are accessed and manipulated through instances (objects) of a class. They can access and modify instance-specific data and behaviours, allowing for customization and polymorphism. Instance methods define actions and behaviours that are specific to individual instances of a class.

Updated on: 28-Jul-2023

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements