PHP Interfaces
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
Interfaces Implement करना
एक class implements keyword इस्तेमाल करके किसी interface के contract से agree करती है, और अगर class interface की माँग वाला हर method define करने में fail हो तो PHP runtime पर एक fatal error produce करेगा।
उदाहरण: Implementing Interfaces
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
कई Interfaces
Classes के उलट, PHP एक single class को एक साथ कई interfaces implement करने देता है, जो PHP का multiple inheritance की flexibility हासिल करने का तरीका है बिना उन ambiguity problems के जो true multiple class inheritance पैदा करेगी।
उदाहरण: Multiple Interfaces
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
Interface Inheritance
Interfaces code को decouple करने के लिए खासतौर पर valuable हैं: एक function जो एक Countable interface accept करता है, उदाहरण के लिए, किसी भी ऐसे object के साथ काम कर सकता है जो count() सही से implement करता है, चाहे यह actually कौन सी concrete class हो।
उदाहरण: Interface Inheritance
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Interfaces बनाम Abstract Classes
PHP खुद Iterator, ArrayAccess, और Countable जैसे कई built-in interfaces के साथ आता है जो आपके अपने custom objects को foreach loops में या count() जैसे functions के साथ native arrays जैसा behave करने देते हैं।
उदाहरण: Interfaces vs. Abstract Classes
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- interface का हर method implement न करना, जो एक fatal error है।
- interface methods को public के अलावा कुछ और declare करना।
- किसी interface में properties डालने की कोशिश करना, जबकि यह सिर्फ methods और constants declare कर सकता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: