← Back to PHP Course | Chapter 8: OOP | Lesson 8 of 14

PHP Interfaces

एक interface एक rule list जैसा है जो कहती है 'जो कोई भी join करना चाहे उसे ये चीज़ें करने में सक्षम होना चाहिए'। एक class जो sign up करती है वह उन abilities में से हर एक होने का promise करती है।
Syntax
php
interface InterfaceName {
    function method_name();
}

class ClassName implements InterfaceName, OtherInterface {
    function method_name() {
        // implementation
    }
}

एक Interface क्या है?

एक interface method signatures का एक contract define करता है जो किसी भी implementing class को देना ज़रूरी है, बिना खुद कोई implementation details specify किए — यह बताता है कि एक class को क्या करना चाहिए, कैसे नहीं।

उदाहरण: What is an Interface?

php
<?php
// Define the interface `Shape` describing required methods
interface Shape {
    // Define the function `area` with no parameters
    function area();
}
// Define the class `Circle`, implementing `Shape`
class Circle implements Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `3.14 * 5 * 5` from this function
        return 3.14 * 5 * 5;
    }
}
// Create a new `Circle` instance, stored in `$c`
$c = new Circle();
// Print `$c->area()` to the output
echo $c->area();
?>

Interfaces Implement करना

एक class implements keyword इस्तेमाल करके किसी interface के contract से agree करती है, और अगर class interface की माँग वाला हर method define करने में fail हो तो PHP runtime पर एक fatal error produce करेगा।

उदाहरण: Implementing Interfaces

php
<?php
// Define the interface `Shape` describing required methods
interface Shape {
    // Define the function `area` with no parameters
    function area();
}
// Define the class `Square`, implementing `Shape`
class Square implements Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `9` from this function
        return 9;
    }
}
// Create a new `Square` instance, stored in `$sq`
$sq = new Square();
// Print `$sq->area()` to the output
echo $sq->area();
?>

कई Interfaces

Classes के उलट, PHP एक single class को एक साथ कई interfaces implement करने देता है, जो PHP का multiple inheritance की flexibility हासिल करने का तरीका है बिना उन ambiguity problems के जो true multiple class inheritance पैदा करेगी।

उदाहरण: Multiple Interfaces

php
<?php
// Define the interface `Swims` describing required methods
interface Swims {
    // Define the function `swim` with no parameters
    function swim();
}
// Define the interface `Flies` describing required methods
interface Flies {
    // Define the function `fly` with no parameters
    function fly();
}
// Define the class `Duck`, implementing `Swims, Flies`
class Duck implements Swims, Flies {
    // Define the function `swim` with no parameters
    function swim() { echo "Swimming\n"; }
    // Define the function `fly` with no parameters
    function fly() { echo "Flying\n"; }
}
// Create a new `Duck` instance, stored in `$duck`
$duck = new Duck();
$duck->swim();
$duck->fly();
?>

Interface Inheritance

Interfaces code को decouple करने के लिए खासतौर पर valuable हैं: एक function जो एक Countable interface accept करता है, उदाहरण के लिए, किसी भी ऐसे object के साथ काम कर सकता है जो count() सही से implement करता है, चाहे यह actually कौन सी concrete class हो।

उदाहरण: Interface Inheritance

php
<?php
// Define the class `Cart`, implementing `Countable`
class Cart implements Countable {
    // Declare the private property `$items`, set to `["a", "b", "c"]`
    private $items = ["a", "b", "c"];
    // Define the function `count` with no parameters
    function count() {
        // Return `count($this->items)` from this function
        return count($this->items);
    }
}
// Create a new `Cart` instance, stored in `$cart`
$cart = new Cart();
// Print `count($cart)` to the output
echo count($cart);
?>

Interfaces बनाम Abstract Classes

PHP खुद Iterator, ArrayAccess, और Countable जैसे कई built-in interfaces के साथ आता है जो आपके अपने custom objects को foreach loops में या count() जैसे functions के साथ native arrays जैसा behave करने देते हैं।

उदाहरण: Interfaces vs. Abstract Classes

php
<?php
// Define the class `Playlist`, implementing `Iterator`
class Playlist implements Iterator {
    // Declare the private property `$songs`, set to `["Song A", "Song B"]`
    private $songs = ["Song A", "Song B"];
    // Declare the private property `$position`, set to `0`
    private $position = 0;
    // Define the function `current` with no parameters
    function current() { return $this->songs[$this->position]; }
    // Define the function `key` with no parameters
    function key() { return $this->position; }
    // Define the function `next` with no parameters
    function next() { $this->position++; }
    // Define the function `rewind` with no parameters
    function rewind() { $this->position = 0; }
    // Define the function `valid` with no parameters
    function valid() { return isset($this->songs[$this->position]); }
}
// Loop over `new Playlist()`, binding each item to `$song`
foreach (new Playlist() as $song) {
    // Print `$song . "\n"` to the output
    echo $song . "\n";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. interface का हर method implement न करना, जो एक fatal error है।
  2. interface methods को public के अलावा कुछ और declare करना।
  3. किसी interface में properties डालने की कोशिश करना, जबकि यह सिर्फ methods और constants declare कर सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.