PHP में Interface , Abstraction achieve करने का दूसरा तरीका है। Interface में defined सभी methods abstract होते हैं इसलिए जो भी class इसको implement करेगी उस class को सभी methods का implementation provide करना पड़ेगा।

PHP में Interface को Class की तरह ही define किया जाता है , बस class keyword का use न interface keyword का use किया जाता है। Interface में methods का content define नहीं किया जाता है।


Interface में defined सभी methods public होने चाहिए , ताकि इन्हे class के बाहर से भी access किया जा सके। और इन methods को किसी class के द्वारा ही implement किया जायगा।

PHP Interface Example

File : php_interface.php

CopyFullscreenClose Fullscreen
<?php 
	interface Animal
	{
	  public function dog();
	}

	class MyClass implements Animal
	{
	  public function dog()
	  {
	    echo 'Dogs bark';
	  }
	}

	$obj = new MyClass();
	$obj->dog();
?>
Output
Dogs bark

Important Notes About Interface

  1. Abstract Class की तरह Interface का Object नहीं बना सकते हैं।

  2. Interface में आप Abstract Class की तरह __construct() & __destruct() का use नहीं कर सकते है।

  3. Interface में defined सभी methods का implementation required है।

  4. Interface में defined सभी methods public होने चाहिए , अगर आप visibility define नहीं करते तो भी By Default public ही होते हैं। punlic नहीं define करने पर fatal error generate होती है।

    Fatal error:Access type for interface method ClassName::Methodname() must be omitted.

  5. Interface में आप member variables define (static , non - static) नहीं कर सकते हैं। हाँ लेकिन आप constant variables define कर सकते हैं। अगर आप normal variable define करते हैं तो fatal error generate होगी।

    Fatal error: Interfaces may not include member variables

Why a class can implement more than on Interface ?

एक single class एक से ज्यादा Interfaces को implement कर सकती है , इसका सबसे बड़ा reason भी है , हम जानते हैं कि super class के constructor run करने के लिए parent::__construct() का use किया जाता है। अब क्योंकि Interface में constructor और destructor define नहीं कर सकते हैं इसलिए जो class इसे implement करती है बो आसानी से constructor और destructor use कर सकती है।


Interface में constructor और destructor define करने से इसके implementation class में जब parent::__construct() call किया जाता तो ये confusion रहती की किस super Interface का constructor call होगा।

इसके अलावा आप अलग - अलग Interfaces में same method भी define कर सकते हैं , लेकिन methods का signature same होना चाहिए , means methods के parameters , name same होने चाहिए।

See Example

File : php_interface2.php

CopyFullscreenClose Fullscreen
<?php 
	interface Animal
	{
	  public function dog();
	}

	interface OtherAnimal
	{
	  /*defining same method*/
	  public function dog();

	}

	class MyClass implements Animal,OtherAnimal
	{
	  public function dog()
	  {
	    echo 'Dogs bark';
	  }
	}

	$obj = new MyClass();
	$obj->dog();
?>
Output
Dogs bark

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers