PHP Interface | Object Oriented Concepts

An Interface in PHP allows you to create methods which a class must implement, without giving any details about its implementation. They resemble abstract methods in an abstract class. However, unlike Abstract Classes you don’t need to provide the implementation of methods in an Interface. In this article, we will discuss PHP Interfaces along with a few examples.

Creating Interface in PHP

Similar to creating classes, you can create an Interface in PHP by using the Interface keyword.

<?php
	interface BaseInterface {
		public function compulsoryMethodA();
		public function compulsoryMethodB($value1);
	}
?>

Characteristics of PHP Interface

The Interfaces have the following characteristics:

  • Methods in a PHP Interface have no implementation. They only have methods.
  • All methods in Interfaces are public. You cannot declare private or protected methods.
  • In PHP, a class can inherit only from one parent class. However, it can implement multiple Interfaces.
  • PHP Interfaces cannot contain member variables. However, they can declare Class Constants.

Implementing a PHP Interface

You can implement an Interface in PHP using the implements keyword. The class then needs to define each and every method declared in the Interface.

<?php
	
	interface BaseInterface {
		public function compulsoryMethodA();
		public function compulsoryMethodB();
	}

	class TestClass implements BaseInterface {
		public function compulsoryMethodA() {
			//Defining Method A
		}
		public function compulsoryMethodB() {
			//Defining Method B
		}
	}
	
?>

A class which implements an interface is called a Concrete Class. It needs to define all the methods declared in the Interface. Also, a class cannot implement an interface of the same name because of ambiguity.

Extending Interface

Similar to Classes, an Interface can also inherit from another Interface using the extend keyword. If any class implements an interface, it needs to define methods in all the interfaces.

<?php
	interface BaseInterface {
		public function compulsoryMethodA();
		public function compulsoryMethodB();
	}

	interface ChildInterface extends BaseInterface {
		public function compulsoryMethodC();
	}

?>

Unlike Classes in PHP, multiple Inheritance in possible in PHP Interfaces.

<?php
	interface A {
		public function methodA();
	}
	interface B {
		public function methodB();
	}
	interface C extends A, B {
		public function methodC();
	}
?>

Constants in Interface

Although Interfaces cannot contain member variables. However, you can define Class Constants in them. Any class implementing that PHP Interface will be able to access the constants with the self keyword.

<?php
	interface BaseInterface {
		CONST INTERFACE_CONSTANT = 'Interface';
	}

	class TestClassA implements BaseInterface {
		public function methodA() {
			echo self::INTERFACE_CONSTANT;
		}
	}
	$testObj = new TestClassA();
	$testObj->methodA();
        /* Interface */
?>

Abstract Classes Vs Interface

In simple terms, an Interface is like a promise. If I say that I implement your interface, I promise that I will implement all the methods declared by you. On the other hand, an Abstract Class is a partially built class. If I say that I extend your Abstract Class, I mean that I may use the properties and methods already defined by you and will define the mandatory methods.

Infographic on Abstract Classes Vs PHP Interface on Concatly
Abstract Classes Vs Interface

Conclusion

In conclusion, we discussed Interface in PHP. We learned about their characteristics and functioning. Also, we discussed the differences between Abstract Classes and Interface. You can learn more about them on the Official Documentation of PHP.

Follow other Articles on Object Oriented Concepts in PHP on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *