Interface in php

In interface, we are using interface keyword in place of class keyword. Interface have methods without implementations which means that methods are abstract methods. All methods should be public visibility. Class can implement multiple interfaces and when a child can use interface using ‘implement’ keyword.

When a class implements any interface then that class must be implement all methods of interface.

interface ExInterface{ 

	public function exMethod1(); 
	public function exMethod2(); 

} 

class ExClass implements ExInterface{ 

	public function exMethod1(){ 
		echo "Ex Method1 Called" . "\n"; 
	} 

	public function exMethod2(){ 
		echo "Ex Method2 Called". "\n"; 
	} 
} 

$obj = new ExClass; 
$obj->exMethod1(); 
$obj->exMethod2(); 

Output

Ex Method1 Called
Ex Method2 Called

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow by Email
LinkedIn
Share
Instagram