php interface vs abstract class

What is an Abstract Class in PHP?
Traits in PHP

In this extensive lesson, we will delve into the complexities of PHP’s interface functionality. From In this post, we’ll look at the differences and similarities between two crucial PHP concepts: interfaces vs abstract classes. We’ll look at the syntax and use cases for each, as well as the benefits and drawbacks of utilizing interfaces and abstract classes in your code. By the conclusion of this session, you’ll know when to use PHP interfaces and when to utilize abstract classes, as well as how to apply them in your programming projects.

What is an interface in OOP?

  • An interface is a class that contains only abstract methods.
  • By using the interface class, we can specify which methods a class should implement that extend the interface.
  • Unlike abstract class, you don’t need to write abstracts with the methods because an interface can only have abstract methods in it.
  • interface keyword is used to declare an interface class. Look at the following syntax of an interface class in PHP.
<?php
interface InterfaceName {
  public function someMethod1();
  public function someMethod2($name, $color);
  public function someMethod3() : string;
}
?>

Though abstract classes and interfaces are quite similar they are logically different from each other.

Difference between Interface and Abstract classes in PHP

Interface ClassAbstract Class
Cannot contain propertiesCan contain properties
All methods are abstract and abstract keyword is not required along with methods.Con contains both abstract as well as non-abstract methods. Abstract keyword is required to declare abstract method.
A class can implement an interface along with inheriting from another class. It means a child class can implement an interface at the same time.A child class (inherited from another class), cannot implement the abstract class.
All methods must be public with respect to access modifiers.Methods can either be public or protected with respect to access modifiers.
Interfaces vs Abstract Classes

Example of Interface in PHP

interface keyword is used to declare an interface in PHP. Look at the following implementation of the interface in PHP.

<?php
interface Unit {
  public function standard_unit();
}

class Weight implements Unit {
  public function standard_unit() {
    echo "Kg";
  }
}

$weight= new Weight();
$weight->standard_unit();
?>
  • In the above example. we create an interface class Unit that contains a method standard_unit.
  • Then we create another class weight that implements the Unit interface and defines the standard_unit method in it.
  • From the above example, we can see that the class that use interface can override its method and can use it in its own way. Let’s say we have another class length, it can use the standard_unit method in its own way.
  • We can write the logic which will work for all physical quantities that have SI units. Look at the following example.
<?php
// Interface definition
interface Unit {
  public function standard_unit();
}

// Class definitions
class Weight implements Unit {
  public function makeSound() {
    echo " kg";
  }
}

class Length implements Unit {
  public function standard_unit() {
    echo " meter";
  }
}

class Temperature implements Unit {
  public function standard_unit() {
    echo " celcius";
  }
}

// Create a list of Physical Quanitites
$weight = new Weight();
$length = new Length();
$temperature = new Temperature();
$quantities= array($weight, $length, $temperature);

// Tell the quantities to use its standard unit
foreach($quantities as $quantities) {
  $quantitiy->standard_unit();
}
?>
  • In the above example, we create similar classes as in the previous example.
  • The weight, length and temperature classes implements the same interface and can use the standard_unit method in its own way. So, we make an array of the objects of these classes and loop them one by one to class the standard_unit method.

Reference to the official PHP 8 interface documentation.

What is an Abstract Class in PHP?
Traits in PHP
en English
X
Scroll to Top