INTERFACES in PHP
interface A {
public function setProperty($x);
public function description();
}
class Mangoes implements A {
public function setProperty($x) {
$this->x = $x;
}
public function description() {
echo 'Describing' . $this->x . tree;
}
}
$Mango = new Mangoes();
$Mango->setProperty(mango);
$Mango->description();
?>
Output:
Describing Mango tree
Interface can be extended with another interface using extends keyword:-
Eg:- <?php
interface A {
public function Compute();
}
interface B extends A {
public function Divide();
}
class C implements B {
public function Divide() {
$var=10;
$var1=2;
$var3=$var/$var1;
echo “division of 10/2 is” . $var3;
}
public function Compute() {
$a=2;
$b=3;
$c=$a*$b;
echo “multiplication of 2*3 is” . $c;
}
}
$obj = new C();
$obj->Divide();
$obj->Compute();
?>
Output:
division of 10/2 is 5
multiplication of 2*3 is 6
An interface is a description of the actions that an object can do. Interface is written in the same way as the class the declaration with interface keyword.
Rules of Interfaces:- All methods declared in an interface must be public; this is the nature of an interface.
- All methods in the interface must be implemented within a class; failure to do so will result in a fatal error.
- The class implementing the interface must use the exact same method signatures as are defined in the interface Interfaces can be extended like classes using the extends operator.
interface A {
public function setProperty($x);
public function description();
}
class Mangoes implements A {
public function setProperty($x) {
$this->x = $x;
}
public function description() {
echo 'Describing' . $this->x . tree;
}
}
$Mango = new Mangoes();
$Mango->setProperty(mango);
$Mango->description();
?>
Output:
Describing Mango tree
Interface can be extended with another interface using extends keyword:-
Eg:- <?php
interface A {
public function Compute();
}
interface B extends A {
public function Divide();
}
class C implements B {
public function Divide() {
$var=10;
$var1=2;
$var3=$var/$var1;
echo “division of 10/2 is” . $var3;
}
public function Compute() {
$a=2;
$b=3;
$c=$a*$b;
echo “multiplication of 2*3 is” . $c;
}
}
$obj = new C();
$obj->Divide();
$obj->Compute();
?>
Output:
division of 10/2 is 5
multiplication of 2*3 is 6
0 comments
Post a Comment