PHP OOP interface

Interface allows us to implement multiple classes in a single class, and multiple inheritances can be implemented using the interface.

PHP interface some properties

  1. Interface not inherited alway implemented
  2. Interface can not contain properties
  3. Interface have abstract methods, need to implemented in derived class
Example
interface Animal{
	public function animal_sound();
}

interface AnimalCanEat{
	public function animal_can_eat();
}

class Dog implements Animal, AnimalCanEat{
	public function animal_sound(){
		echo "Dog can bark <br>";
	}
	public function animal_can_eat(){
		echo "Dog can eat <br>";
	}
}

$dog= new Dog();
$dog->animal_sound();
$dog->animal_can_eat();
© 2016 - 2022, All Rights are Reserved.