परिचय
इंटरफेस ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग की एक महत्वपूर्ण विशेषता है जिसके द्वारा यह निर्दिष्ट करना संभव है कि एक क्लास को कौन से मेथड्स इम्प्लीमेंट करने चाहिए, बिना यह परिभाषित किए कि उन्हें कैसे इम्प्लीमेंट किया जाना चाहिए।
PHP interface कीवर्ड द्वारा इंटरफेस का समर्थन करता है। इंटरफेस क्लास के समान होता है लेकिन बिना डेफिनिशन बॉडी के मेथड्स के साथ। इंटरफेस में मेथड्स public होने चाहिए। एक इनहेरिटेड क्लास जो इन मेथड्स को इम्प्लीमेंट करती है, उसे implements कीवर्ड के साथ परिभाषित किया जाना चाहिए (extends के बजाय), और पैरेंट इंटरफेस के सभी मेथड्स के इम्प्लीमेंटेशन प्रदान करने चाहिए।
सिंटैक्स
<?php
interface testinterface {
public function testmethod();
}
class testclass implements testinterface {
public function testmethod() {
echo "implements interface method";
}
}
?>
इंटरफेस के सभी मेथड्स इम्प्लीमेंटिंग क्लास द्वारा परिभाषित किए जाने चाहिए, अन्यथा PHP पार्सर एक्सेप्शन फेंकता है।
उदाहरण: मेथड मिस होने पर त्रुटि
<?php
interface testinterface {
public function test1();
public function test2();
}
class testclass implements testinterface {
public function test1() {
echo "implements interface method";
}
}
$obj = new testclass();
?>
आउटपुट
PHP Fatal error: Class testclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testinterface::test2)
एक्सटेंडेबल इंटरफेस
सामान्य क्लास की तरह, एक इंटरफेस को भी extends कीवर्ड का उपयोग करके इनहेरिट किया जा सकता है।
<?php
interface testinterface {
public function test1();
}
interface myinterface extends testinterface {
public function test2();
}
class testclass implements myinterface {
public function test1() {
echo "implements test1 method";
}
public function test2() {
echo "implements test2 method";
}
}
?>
इंटरफेस का उपयोग करके मल्टीपल इनहेरिटेंस
PHP एक से अधिक क्लास को extends क्लॉज में अनुमति नहीं देता। हालांकि, मल्टीपल इनहेरिटेंस को चाइल्ड क्लास द्वारा एक या अधिक इंटरफेस इम्प्लीमेंट करके प्राप्त किया जा सकता है।
निम्न उदाहरण में, myclass testclass को एक्सटेंड करता है और testinterface को इम्प्लीमेंट करता है ताकि मल्टीपल इनहेरिटेंस प्राप्त हो सके:
<?php
interface testinterface {
public function test1();
}
class testclass {
public function test2() {
echo "this is test2 function in parent class\n";
}
}
class myclass extends testclass implements testinterface {
public function test1() {
echo "implements test1 method\n";
}
}
$obj = new myclass();
$obj->test1();
$obj->test2();
?>
आउटपुट
implements test1 method this is test2 function in parent class
व्यावहारिक उदाहरण: Shape इंटरफेस
<?php
interface shape {
public function area();
}
class circle implements shape {
private $rad;
public function __construct() {
$this->rad = 5;
}
public function area() {
echo "area of circle=" . M_PI * pow($this->rad, 2) . "\n";
}
}
class rectangle implements shape {
private $width;
private $height;
public function __construct() {
$this->width = 20;
$this->height = 10;
}
public function area() {
echo "area of rectangle=" . $this->width * $this->height . "\n";
}
}
$c = new circle();
$c->area();
$r = new rectangle();
$r->area();
?>
आउटपुट
area of circle=78.539816339745 area of rectangle=200
मुख्य बिंदु
- इंटरफेस केवल मेथड सिग्नेचर परिभाषित करते हैं, इम्प्लीमेंटेशन नहीं।
- एक क्लास multiple interfaces इम्प्लीमेंट कर सकती है (कॉमा से अलग करके)।
- इंटरफेस constants परिभाषित कर सकते हैं लेकिन प्रॉपर्टीज़ नहीं।
- PHP 8.1+ में इंटरफेस में static मेथड्स भी हो सकते हैं।