Interface basics php oops

In PHP, an interface is defined with “Interface“ keyword followed by the name of interface.

An interface can not contain “constant variables” as well as “normal variables”properties ).

All the methods which are declared in an “interface” must be “public”.

Only “Declaration” of “methods” are done in an “interface“.

All the methods must be implemented within a class.

The class which implements methods of the interface must have same signature of methods.

we can not create object of the interface.

An interface is a ‘pure abstract class’.

One interface can extends another interface.

Example 1:


<?php

	  /* interface A declaration */
	  interface A {
		
			public function Compute();
			
	  }
			
	 /* interface B extends interface A */
	 interface B extends A{
		
			/* member function declaration */
			public function Divide();
			
	  }
		
	 /* class C implements Class B */
	 class C implements B {
		
			/* function body */
			public function Divide() {
			
				$var = 10;
				$var1 = 2;
				$var3 = $var/$var1;
				echo 'division of 10/2 is'.' ' . $var3.'<br>';
				
			}
			
			/* function Body */
			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.
		
		

Example 2:


<?php

		/* interface declaration */
		interface A {
		
			/* Member function declaration */
			public function setProperty($x);
			public function description();
			
		}
		interface B extends A {
		
		}
		
		/* Class Mangoes implements interface A */
		class Mangoes implements A{
		
			/* Member function */
			public function setProperty($x){
			
			  $this->x = $x;
			  
			}
			
			/* Member function */
			public function description(){
			
			  echo 'Describing' .' '. $this->x .' '.'tree';
			  
			}
		}
			
		$Mango = new Mangoes();
		$Mango->setProperty('mango');
		$Mango->description();
		
?>

------------ ****** ------------

OUTPUT:
         Describing mango tree.